Home | Articles|Namespace|Interview Questions|Tools|Jobs|Projects|Community
Asp.net Tutorials

»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books

»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects

»Competition
Community
Winners
Prizes
Write For Us
Members

»Other Resources
Links
Dotnet Resources

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.

Have a Question and dont know the answer post it below and get answers in minutes

Due to spam this feature is disabled
To get answers fast , make sure you enter a detailed subject for example: "DataGrid issues need answer" not "DataGrid"

Subject:

Catjegory Name:

Message:



© 2008 dotnetwatch.com -- Privacy policy