How to call the user control method in the page in asp.net

asp.net

I have the Following method in Usercontrol.ascx

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

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

I need the following method to be called from another Page and another Usercontrol.ascx

Best Answer

You need to get a reference to the instance of the UserControl on your page, then call the method, like so:

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

}