简体   繁体   English

如何使用asp.net从另一个usercontrol中调用usercontrol的方法?

[英]How can i call method from usercontrol from another usercontrol using asp.net?

I have two usercontrols: UserControl1 and UserControl2 , these controls are added in the Page.aspx. 我有两个usercontrols: UserControl1UserControl2 ,这些控件添加在Page.aspx中。

UserControl1 : This usercontrol contains method for hidden textboxes in this usercontrol. UserControl1 :此usercontrol包含此usercontrol中隐藏文本框的方法。 This method is called " HideTextbox " 此方法称为“ HideTextbox

UserControl2 : I want to call method " HideTextBox " from UserControl2 . UserControl2 :我想从UserControl2调用方法“ HideTextBox ”。

How can I call method HideTextBox from UserControl2 ? 如何从UserControl2调用方法HideTextBox

Only if both are usercontrols or servercontrols, or you're looking for a servercontrol from a usercontrol will this work. 只有当两者都是usercontrols或servercontrols,或者你正在寻找来自usercontrol的servercontrol时,这才有效。 (not from a servercontrol because you can't get a reference to the asp.usercontrol_xx assembly) (不是来自servercontrol,因为你无法获得对asp.usercontrol_xx程序集的引用)

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.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;
}

Once you have your Usercontrol2 reference, you can easily call your public method. 获得Usercontrol2参考后,您可以轻松调用您的公共方法。

This problem can be solved by creating a custom event in UC2 and consuming that event on the main page to invoke the hide method on UC1. 可以通过在UC2中创建自定义事件并在主页面上使用该事件来调用UC1上的hide方法来解决此问题。

You declare a delegate in your user control 您在用户控件中声明了一个委托

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

Then define the event for the delegate you created 然后为您创建的委托定义事件

public event HideTextBoxEventHandler HideTextBox;

At the point in the code where you want to hide text box you need to invoke that event: 在代码中您要隐藏文本框的位置,您需要调用该事件:

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);
}

Then from the main page create an event handling method 然后从主页面创建一个事件处理方法

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

You will need to add to the page load or where ever you are loading UC2 您需要添加到页面加载或加载UC2的位置

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

I would declare interface on control that knows how to hide text boxes, something like this : 我会声明控件上的接口知道如何隐藏文本框,如下所示:

public interface ITextBoxHider
{
  void HideTextBoxes();
}

after that implement this interface on UserControl1, when you want to hide text boxes enumerate all controls on a form and find one that implements ITextBoxHider, and then simply call that method : 在UserControl1上实现此接口之后,当您要隐藏文本框时,枚举表单上的所有控件并找到一个实现ITextBoxHider的控件,然后只需调用该方法:

helper method that will enumerate all controls : 将枚举所有控件的辅助方法:

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;
  }
}

using that method and calling HideTextBoxes from UserControl2 : 使用该方法并从UserControl2调用HideTextBoxes:

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

On user control 2 在用户控制2

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

or shorten 或缩短

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

On user control 1 在用户控制1

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

First you need to make HideTextBox() a public method, not private. 首先,您需要将HideTextBox() public方法,而不是私有方法。

Then call UserControl1.HideTextBox() . 然后调用UserControl1.HideTextBox()

Update: I wasn't clear about getting the reference to UserControl1. 更新:我不清楚是否获得对UserControl1的引用。 When I did this in my project, I created an interface that declared the method I was going to call, so my code was like this: 当我在我的项目中执行此操作时,我创建了一个声明我要调用的方法的接口,所以我的代码是这样的:

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

Make sure your UserControl1 derives from the interface: public partial class UserControl1 : System.Web.UI.UserControl, IUserControl1 确保您的UserControl1派生自接口:public partial class UserControl1:System.Web.UI.UserControl,IUserControl1

If you want to access method using object of Usercontrol, you have to register it like this.. 如果你想使用Usercontrol的对象访问方法,你必须像这样注册它..

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

You have to declare HideTextBox() as public. 您必须将HideTextBox()声明为public。

Also, you need to add a directive on a webform telling the webform that you are going to dynamically load the usercontrol. 此外,您需要在webform上添加一个指令,告诉webform您将动态加载usercontrol。 So in the webform, add the following directive. 因此,在webform中,添加以下指令。

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

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从asp.net中的另一个用户控件调用usercontrol中的方法 - To call a method in usercontrol from another user control in asp.net 如何使用事件从UserControl调用方法到另一个UserControl或另一个aspx.Page? - How can i call method from UserControl to another UserControl or another aspx.Page using Events? 我如何使用C#从另一个用户控件的用户控件调用方法? - How can i call method from usercontrol from another usercontrol using c#? 如何从asp.net中的UserControl调用父方法? - How to Call a Parent Method from UserControl in asp.net? 如何从asp.net中的子UserControl调用父UserControl方法? - How to call parent UserControl method from a child UserControl in asp.net? 如何使用ASP.NET从另一个用户控件访问一个UserControl - How To Access One UserControl from Another Using ASP.NET 如何从usercontrol调用aspx内容页面方法? ASP.NET,C# - How can I call aspx content page methods from usercontrol? ASP.NET, C# 如何从usecontrol调用方法到另一个usercontrol? - how do i call a method from a usecontrol to a another usercontrol? Asp.Net:在更新面板中从用户控件重定向到另一个页面 - Asp.Net : Redirect to another page from usercontrol in update panel Asp.Net usercontrol 代码隐藏:从另一个用户控件引用嵌套的 class - Asp.Net usercontrol codebehind: reference nested class from another usercontrol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM