2010-04-23 51 views
3

我在頁面上標記有FormView在找不到FormView.InsertItemTemplate控制甚至在DataBound事件

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

BECAME

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上找到控制權。但我不是。爲什麼?

回答

5
If (FormView1.CurrentMode == FormViewMode.Insert) 
     var c = FormView1.FindControl("ucUserControl1"); 
+0

我編輯我的答案更清晰 – abatishchev 2010-04-23 16:40:51

+0

嘗試更新的代碼 – Glennular 2010-04-23 16:43:26

+0

我更新的代碼可以是一個解決方法嗎?或者哪裏是改變模式的最佳地點? 'DataBiding','DataBound'? – abatishchev 2010-04-23 16:50:42