【问题标题】:How can i call method from usercontrol from another usercontrol using asp.net?如何使用 asp.net 从另一个用户控件调用用户控件的方法?
【发布时间】:2013-01-17 19:27:16
【问题描述】:

我有两个用户控件:UserControl1UserControl2,这些控件是在 Page.aspx 中添加的。

UserControl1: 此用户控件包含此用户控件中隐藏文本框的方法。这个方法叫做“HideTextbox

UserControl2: 我想从UserControl2 调用方法“HideTextBox”。

如何从UserControl2 调用方法HideTextBox

【问题讨论】:

  • UserControl2 是 UserControl1 中的子控件吗?
  • 没有。 UserControl 2 与 UserControl1 处于同一级别 - 在 Page.aspx 中。

标签: c# asp.net user-controls


【解决方案1】:

只有当两者都是用户控件或服务器控件,或者您正在从用户控件中寻找服务器控件时,此方法才会起作用。 (不是来自服务器控件,因为您无法获得对 asp.usercontrol_xx 程序集的引用)

首先获取对父页面的引用(通常可以使用this.Parent 来完成。 接下来对父级执行递归 FindControl 以找到类型为UserControl2 的控件。 示例代码:

//this for extension method
public static UserControl2 FindUserControl2Recursive(this Control root)
{
    var uc2 = root as ASP.UserControls_UserControl2_ascx;
    if (uc2 != null)
    {
        return uc2;
    }
    foreach (var control in root.Controls)
    {
        uc2 = control.FindUserControl2Recursive();
        if (uc2 != null)
        {
            return uc2;
        }
    }
    return null;
}

一旦您有了 Usercontrol2 引用,您就可以轻松地调用您的公共方法。

【讨论】:

    【解决方案2】:

    可以通过在 UC2 中创建自定义事件并在主页上使用该事件来调用 UC1 上的隐藏方法来解决此问题。

    您在用户控件中声明一个委托

    public delegate void HideTextBoxEventHandler(object sender, EventArgs e);
    

    然后为您创建的委托定义事件

    public event HideTextBoxEventHandler HideTextBox;
    

    在代码中要隐藏文本框的位置,您需要调用该事件:

    if (this.HideTextBox != null) // if there is no event handler then it will be null and invoking it will throw an error.
    {
       EventArgs e = new EventArgs();
       this.HideTextBox(this, e);
    }
    

    然后从主页面创建一个事件处理方法

    protected void UserControl2_HideTextBox(Object sender, EventArgs e)
    {
       UC1.InvokeHideTextBox();  // this is the code in UC1 that does the hiding
    }
    

    您需要添加到页面加载或加载 UC2 的任何位置

    UC2.HideTextBox += new UserControl2.HideTextBoxEventHandler(this.UserControl2_HideTextBox);
    

    【讨论】:

      【解决方案3】:

      我会在知道如何隐藏文本框的控件上声明接口,如下所示:

      public interface ITextBoxHider
      {
        void HideTextBoxes();
      }
      

      在 UserControl1 上实现这个接口之后,当你想隐藏文本框时,枚举一个窗体上的所有控件并找到一个实现 ITextBoxHider 的控件,然后简单地调用该方法:

      枚举所有控件的辅助方法:

      public static IEnumerable<Control> GetAllChildren(Control parent)
      {
        foreach (Control control in parent.Controls)
        {
          foreach (Control grandChild in GetAllChildren(control))
              yield return grandChild;
      
          yield return control;
        }
      }
      

      使用该方法并从 UserControl2 调用 HideTextBoxes :

      var hider = GetAllChildren(this.Page).FirstOrDefault(ct => ct is ITextBoxHider);
      if (hider != null)
        (hider as ITextBoxHider).HideTextBoxes();
      

      【讨论】:

        【解决方案4】:

        在用户控件 2 上

        UC1Class UserControl = Parent.FindControl("UC1_Name") as UC1Class;
        UserControl.HideTextbox();
        

        或缩短

        (Parent.FindControl("UC1_Name") as UC1Class).HideTextbox();
        

        在用户控件 1 上

        public void HideTextbox()
        {
            ....Do Something
        }
        

        【讨论】:

          【解决方案5】:

          如果你想使用用户控件的对象访问方法,你必须像这样注册它..

          UserControl1 uc = new UserControl1();
          uc.HideTextBox();
          

          您必须将 HideTextBox() 声明为 public。

          此外,您需要在 Web 表单上添加一个指令,告诉 Web 表单您将动态加载用户控件。因此,在 webform 中,添加以下指令。

          <%@ Reference Control = "WebUserControl1.ascx" %>
          

          希望对你有帮助

          【讨论】:

          • UC1 和 UC2 都存在于一个页面上,但彼此都不知道。调用您描述的方法将调用 UC1 的新实例中的方法,而不是页面上的实例。
          【解决方案6】:

          首先您需要将HideTextBox() 设为public 方法,而不是私有的。

          然后拨打UserControl1.HideTextBox()

          更新: 我不清楚获取对 UserControl1 的引用。当我在我的项目中这样做时,我创建了一个接口来声明我要调用的方法,所以我的代码是这样的:

          IUserControl1 uc1 = (IUserControl1)this.Parent.FindControl("ucOne");
          uc1.HideTextBox();
          

          确保您的 UserControl1 从接口派生: 公共部分类 UserControl1 : System.Web.UI.UserControl, IUserControl1

          【讨论】:

          • 没错,但我认为真正的问题是从UserControl1 获得对UserControl2 的引用。
          • 罗伯特,你是对的。此错误“对象引用未设置为对象的实例”
          猜你喜欢
          • 2015-05-22
          • 2011-03-23
          • 2011-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多