Problem finding control in FormView using code

Here is the code behind ... I'm trying to get this control so that I can add items to the drop-down list (I return the role groups to add them to the drop-down list in the code)

Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim DDRoleGroups As DropDownList
    DDRoleGroups = FormView1.FindControl("DDRoleGroup")
End Sub

Here's FormView: (I pulled out most of the fields to make it easier to read)

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
     DataSourceID="ObjectDataSource_Vendors" 
     DefaultMode="Insert" BorderColor="DarkGray" 
     BorderStyle="Solid" BorderWidth="1px" CellPadding="4" Visible="False"> 
  <EditItemTemplate> 
  </EditItemTemplate> 
  <InsertItemTemplate>                          
    <label class="form_label">Role Group:</label><br /><asp:DropDownList ID="DDRoleGroup" 
               runat="server" Width="175px"
               EnableViewState="False"> 
              </asp:DropDownList>
   </InsertItemTemplate>
</asp:FormView>

Could this be due to the fact that it is not yet loaded in the Page_Load substrate and in the control?

Thanks
Matt

+4
source share
2 answers

. Formview ModeChanged , CurrentMode == Insert:

protected void FormView1_ModeChanged(object sender, EventArgs e)
{
    if (FormView1.CurrentMode == FormViewMode.Insert)
    {
        DropDownList DDRoleGroups = FormView1.FindControl("DDRoleGroup");
        // fill dropdown
    }
}

Page_Load, .

+3

FindControl formview , FormView "CurrentMode".

FindControl "DDRoleGroups", FormView "", , .

, .

+1

All Articles