【问题标题】:not able to find usercontrol in edittemplate of datalist无法在数据列表的编辑模板中找到用户控件
【发布时间】:2011-07-05 16:45:02
【问题描述】:

我在数据列表的编辑模板中有一个 webusercontrol。在后面的代码中,在 itemCommand 中,当我尝试使用 findcontrol 找到它时,我得到空对象。

我做错了什么?

WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");

或者我也在 E​​ditCommand 事件中尝试了以下操作,因为我将用户控件保留在 DataList 的 EditTemplate 中:

WebUserControl cntrl = (WebUserControl)DataList1.FindControl("myControl");

【问题讨论】:

  • 您的代码示例似乎有效,但如果没有更多信息,确实无法提供明确的答案。
  • 我们需要一些标记来告诉...

标签: asp.net data-controls


【解决方案1】:

我认为你可能正在做这样的事情:

    protected void gridView_rowDataBound(Object sender, GridViewRowEventArgs e)
    {
        WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");
    }

请记住,您将查看每一行 - 包括页眉和页脚行。

我认为你需要这个:

    protected void gridView_rowDataBound(Object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow){
            WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");
       }
    }

【讨论】:

  • 我不是在找 gridview。
【解决方案2】:

其实没关系,我将控件移动到 headertemplate 并查看它的控件集合,我能够使用 FindControl 找到控件。如果我将它放在edititemtemplate中,不知道为什么它找不到。但谢谢大家感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多