formview.insertitemtemplateでコントロールを見つけることができません。データバウンドイベントでも

StackOverflow https://stackoverflow.com/questions/2700167

質問

私は持っている 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" />

それはコードビハインドです だった:

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 データバインドされた後。でも僕はそうじゃない。なんで?

役に立ちましたか?

解決

If (FormView1.CurrentMode == FormViewMode.Insert)
      var c = FormView1.FindControl("ucUserControl1");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top