3

FormView的页面标记中有:

<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" />

它的代码隐藏WAS

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

成为

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!
   }
}

从理论上讲,我能够在FormView数据绑定后找到控制权。但我不是。为什么?

4

1 回答 1

5
If (FormView1.CurrentMode == FormViewMode.Insert)
      var c = FormView1.FindControl("ucUserControl1");
于 2010-04-23T16:31:37.843 回答