1

我在 Usercontrol.ascx 中有以下方法

public void Flasmessage()
{
    popupmessage2.Visible = true;
    string strScript = "HideCtrl('" + popupmessage2.ClientID + "','15000')";

    Page.ClientScript.RegisterStartupScript(
      this.GetType(),
      Guid.NewGuid().ToString(),
      strScript,
      true);
}

我需要从另一个 Page 和另一个 Usercontrol.ascx 调用以下方法

4

2 回答 2

2

您需要在页面上获取对 UserControl 实例的引用,然后调用该方法,如下所示:

void Page_Load() {

    // do this if your control does not exist in your *.aspx file and needs to be manually added:
    MyUserControl control = (MyUserControl)LoadControl("MyUserControl.ascx");
    this.Controls.Add( control );

    // do this if your control already exists in your page as a named control
    MyUserControl control = (MyUserControl)FindControl("myUserControl");

    // do this in both cases, or if your UserControl exists as a field in your *.aspx-generated page class.
    control.MyMethod();

}
于 2012-11-08T09:38:24.313 回答
-1

在 UserControl 中公开该方法,例如:

public int Calculate()
{

}

然后在页面中调用它如下

int total = MyControl.Calculate();
于 2012-11-08T09:47:30.907 回答