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:
Post a Comment