Search code examples
c#asp.netajaxcontroltoolkitmodalpopupextender

Call (show) a modal popup located in MasterPage from it's childs


I'm trying to make a default modal box that must be accessible from any part of the application, and need to be called whenever I want from inside any page. (must be called from code-behind).

So I came up with the idea of a Panel + modalPopupExtender placed in the MasterPage, and calling it from child pages via code-behind.

How can I do that? Or perhaps you guys have a better idea to solve this.


Solution

  • Since the modal is to be called from the code behind, you can achieve it like this

    Add a method to your Master Page

    public class MyMaster : MasterPage
    {
         public void ShowModal(string someParameter)
         {
              // Do your logic here
              // Show the modal
         }
    }
    

    Then add a method to your page, or page base like this...

    public void ShowModal(string someParameter)
    {
         MyMaster masterPage = this.Master as MyMaster;
         masterPage.ShowModal(someParameter);
    }
    

    I recommend using a base class for your pages so that you don't have to replicate the above method.