Problem finding a control within a FormView from code-behind
Asked Answered
R

2

4

Here the code behind... I'm trying to retrieve this control so I can add items to the drop down list (I'm retrieving the Role Groups to add to the drop down list in the code-behind)

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 the FormView: (I took out most of the fields so it's 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 it possibly have to do with the fact that it's in the Page_Load sub and the control hasn't acctually loaded yet?

Thanks,
Matt

Rearmost answered 24/6, 2009 at 15:39 Comment(0)
S
3

Your dropdown only exists in Insert mode. Try to implement the formview's ModeChanged event and retrieve the control if CurrentMode == Insert:

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

You cannot handle this in Page_Load, as the form has not yet switched into Insert mode.

Serge answered 18/7, 2009 at 9:7 Comment(0)
S
1

FindControl on a formview will only work for the template that the FormView's "CurrentMode" property is set to.

In your case, you can only do FindControl for "DDRoleGroups" if your FormView is set to "Insert", since that's the template that your control exists in.

Hope that helps.

Smithy answered 24/6, 2009 at 15:47 Comment(5)
DefaultMode="Insert" <-- Is that's not the same thing as CurrentMode="Insert"?Rearmost
Not quite. DefaultMode is what the formview returns to after any insert/update/delete operation. CurrentMode represents what mode the FV is in currently. I didn't see you had set your DefaultMode already. If it's set to Insert and you've verified that it is indeed on Insert during Page_Load, then something else is going on.Smithy
Did what you suggested, and even encapsulated the FindControl within If ViewForm1.CurrentMode.Equals("Insert) Then. It definitely went inside the If, so I'm not sure why it wouldn't be working...Rearmost
Can you try FormView.Row.FindControl("DDRoleGroup")?Smithy
Oh, and you might need to do that in PreRender()... check here: msdn.microsoft.com/en-us/library/…Smithy

© 2022 - 2024 — McMap. All rights reserved.