Cannot find controls in FormView.InsertItemTemplate even with DataBound event

I have FormViewin my page layout:

<asp:FormView ruanat="server" ID="FormView1" DataSourceID="SqlDataSource1" OnDataBinding="FormView1_DataBinding" OnDataBound="FormView1_DataBound">
   <InsertItemTemplate>
      <uc:UserControl1 runat="server" ID="ucUserControl1" />
   </InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" SelectCommand="EXEC someSP" />

This is the WAS code :

protected void FormView1_DataBound(object sender, EventArgs e)
{
   var c = FormView1.FindControl("ucUserControl1"); // returns null
}

STEEL

protected void FormView1_DataBinding(object sender, EventArgs e)
{
   FormView1.ChangeMode(FormViewMode.Insert);
}

protected void FormView1_DataBound(object sender, EventArgs e)
{
   if (FormView1.CurrentMode = FormViewMode.Insert)
   {
      var c = FormView1.FindControl("ucUserControl1"); // returns null no more!
   }
}

In theory, I can find a control FormViewafter it is bound to data. But not me. Why?

+3
source share
1 answer
If (FormView1.CurrentMode == FormViewMode.Insert)
      var c = FormView1.FindControl("ucUserControl1");
+5
source

Source: https://habr.com/ru/post/1742490/


All Articles