[英]not able to find usercontrol in edittemplate of datalist
我在數據列表的編輯模板中有一個 webusercontrol。 在后面的代碼中,在 itemCommand 中,當我嘗試使用 findcontrol 找到它時,我得到 null object。
我做錯了什么?
WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");
或者我還在 EditCommand 事件中嘗試了以下操作,因為我將用戶控件保留在 DataList 的 EditTemplate 中:
WebUserControl cntrl = (WebUserControl)DataList1.FindControl("myControl");
我想你可能正在做這樣的事情:
protected void gridView_rowDataBound(Object sender, GridViewRowEventArgs e)
{
WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");
}
請記住,您將查看每一行 - 包括 header 和頁腳行。
我認為你需要這個:
protected void gridView_rowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow){
WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");
}
}
其實沒關系,我將控件移動到 headertemplate 並查看它的控件集合,我能夠使用 FindControl 找到控件。 如果我將它放在edititemtemplate中,不知道為什么它找不到。 但謝謝大家感謝您的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.