Accessing Modal Popup Extender defined in one ASCX page in another ASCX pages Code Behind

StackOverflow https://stackoverflow.com/questions/16843856

  •  30-05-2022
  •  | 
  •  

質問

I have two ASCX pages, call them Page1 and Page2.

In the HTML of Page1, I define a ModalPopupExtender. Now in the code behind on Page2, I'd like to do a ModalPopupExtender.Show when a button is clicked.

When I try to do this, I get an error stating the ModalPopupExtender doesn't exist in current context. Is there a way to reference the ModalPopupExtender defined in Page1 from Page2 so I can control it?

役に立ちましたか?

解決

Expose your modal popup as a public property on your main page.

public class BasePage: System.Web.Ui.Page
{
    public ModalPopupExtender MyPopup
    {
        get
        {
            return this.myPopup;
        }
    }
}

In your ascx code behind cast this.Page to your page type.

public class Page1 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((BasePage)this.Page).MyPopup.DoWhatEver();
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top