【问题标题】:Can't find controls in FormView.InsertItemTemplate even on DataBound event即使在 DataBound 事件中也无法在 FormView.InsertItemTemplate 中找到控件
【发布时间】:2011-02-11 14:41:00
【问题描述】:

我的页面标记中有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 被数据绑定后找到它的控制权。但我不是。为什么?

【问题讨论】:

    标签: .net asp.net formview findcontrol databound


    【解决方案1】:
    If (FormView1.CurrentMode == FormViewMode.Insert)
          var c = FormView1.FindControl("ucUserControl1");
    

    【讨论】:

    • 我的更新代码可以作为一种解决方法吗?或者改变模式的最佳位置在哪里? DataBiding, DataBound ?
    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多