This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Sunday, November 2, 2008

Referencing Controls in Templates after mode changes in Formview

Controls of a template are created dynamically when the template is shown, trying to get a reference to these controls in the typical way will lead to a null reference exception.

Thus, the way to get a reference to a control in a template is using the FindControl method, which looks for controls within the current naming container by its Id

Now suppose that you want to update some controls when the user switches from one FormViewMode to another. For example, you may want to get a DropDownList filled with data when entering Edit mode. At first you may think that handling the ModeChanged event of the FormView is the correct solution.

Sub EmployeeFormView_DataBound(ByVal sender As Object, ByVal e As EventArgs)
If myFormView.CurrentMode = FormViewMode.Edit Then
Dim countries As DropDownList = CType(myFormView.FindControl("countriesDropDownList"), DropDownList)
If countries IsNot Nothing Then
' fill the dropdownlist with data
End If
End If
End Sub

At this moment, all the child controls of the Edit template have already been created and the reference to the countries control is the actual DropDownList you were looking for.

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Blog Archive

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger