我怎样才能调用方法,从用户控件使用asp.net另一个用户控件? [英] How can i call method from usercontrol from another usercontrol using asp.net?

查看:91
本文介绍了我怎样才能调用方法,从用户控件使用asp.net另一个用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用户控件:的UserControl1 UserControl2 ,这些控件在Page.aspx增加。

I have two usercontrols: UserControl1 and UserControl2, these controls are added in the Page.aspx.

的UserControl1
这个用户控件包含在这个用户控件隐藏文本框的方法。这种方法被称为 HideTextbox

UserControl2
我想从 UserControl2 调用方法 HideTextBox

我如何可以调用方法 HideTextBox UserControl2

How can I call method HideTextBox from UserControl2 ?

推荐答案

只有两个是用户控件或servercontrols,或者你正在寻找从用户控件将这项工作一servercontrol。 (不是从servercontrol,因为你不能给asp.usercontrol_xx集的引用)

Only if both are usercontrols or servercontrols, or you're looking for a servercontrol from a usercontrol will this work. (not from a servercontrol because you can't get a reference to the asp.usercontrol_xx assembly)

首先获得父页面的引用(通常这可以用 this.Parent 来完成。
下一步做父母的递归的FindControl找到类型为 UserControl2 控制。
例如:code:

First get a reference to the parent page (usually this can be done with this.Parent. Next do a recursive FindControl on the parent to find a control whose type is UserControl2. Example code:

//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参考,您可以轻松地打电话给你的公共方法。

Once you have your Usercontrol2 reference, you can easily call your public method.

这篇关于我怎样才能调用方法,从用户控件使用asp.net另一个用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆