【问题标题】:In an asp.net, how to get a reference of a custom webcontrol from a usercontrol?在 asp.net 中,如何从 usercontrol 获取自定义 webcontrol 的引用?
【发布时间】:2011-01-30 23:59:27
【问题描述】:

我有一个包含自定义 web 控件和用户控件的页面。我需要在 usercontrol 类中引用 webcontrol。因此,我在页面类中将 webcontrol 声明为 public。但是,当我执行“this.Page.”时,我没有看到智能感知提供的列表中列出的 web 控件。很可能,我错过了一些东西。

在asp.net页面中,如何从usercontrol中获取自定义webcontrol的引用?

请指教。谢谢 潘卡伊

【问题讨论】:

    标签: asp.net user-controls web-parts


    【解决方案1】:

    您可能最好在 UserControl 上创建一个接受 WebControl 参数的方法。

    然后让页面将它们连接在一起。

    拥有一个依赖于 Page.FindControl 的用户控件对我来说似乎很像一个 hack。

    例如在你的 UserControl 中有这样的东西:

    public void SetWebControl(MyWebControl control)
    {
    // Do whatever
    }
    

    在你的页面中你会这样做:

    override OnInit()
    {
    this.MyUserControl1.SetWebControl(MyWebControl1);
    }
    

    都是c的伪代码:-)

    【讨论】:

      【解决方案2】:

      如果你知道 webcontrol 的 ID,你可以这样做:

      Control ctrl = Page.FindControl (ID_of_the_webcontrol);
      

      【讨论】:

        【解决方案3】:

        尝试使用 FindControl 方法控制页面:

        Control yourUserControl = this.Page.FindControl("yourControlsID");
        

        Page 类是您的页面的实例,它是基类System.Web.UI.Page 的实例。因此,除非您将其转换为您的页面类的类型,否则您无法使用 Page 获取公共属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-28
          • 1970-01-01
          • 2013-08-24
          • 1970-01-01
          • 2015-02-26
          • 2019-11-04
          相关资源
          最近更新 更多