簡體   English   中英

如何使用asp.net從另一個usercontrol中調用usercontrol的方法?

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

我有兩個usercontrols: UserControl1UserControl2 ,這些控件添加在Page.aspx中。

UserControl1 :此usercontrol包含此usercontrol中隱藏文本框的方法。 此方法稱為“ HideTextbox

UserControl2 :我想從UserControl2調用方法“ HideTextBox ”。

如何從UserControl2調用方法HideTextBox

只有當兩者都是usercontrols或servercontrols,或者你正在尋找來自usercontrol的servercontrol時,這才有效。 (不是來自servercontrol,因為你無法獲得對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參考后,您可以輕松調用您的公共方法。

可以通過在UC2中創建自定義事件並在主頁面上使用該事件來調用UC1上的hide方法來解決此問題。

您在用戶控件中聲明了一個委托

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

我會聲明控件上的接口知道如何隱藏文本框,如下所示:

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

在用戶控制2

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

或縮短

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

在用戶控制1

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

首先,您需要將HideTextBox() public方法,而不是私有方法。

然后調用UserControl1.HideTextBox()

更新:我不清楚是否獲得對UserControl1的引用。 當我在我的項目中執行此操作時,我創建了一個聲明我要調用的方法的接口,所以我的代碼是這樣的:

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

確保您的UserControl1派生自接口:public partial class UserControl1:System.Web.UI.UserControl,IUserControl1

如果你想使用Usercontrol的對象訪問方法,你必須像這樣注冊它..

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

您必須將HideTextBox()聲明為public。

此外,您需要在webform上添加一個指令,告訴webform您將動態加載usercontrol。 因此,在webform中,添加以下指令。

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

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM