8

I have a masterpage and a content page. In the content page I have a script manager and an update panel. In the update panel I want to be able to click a button which would hit a public method on the master page to show a message. This works if I don't have an update panel on the content page but is there a way to get it to work when the button is in an update panel?

Master Page:

public void ShowMessage(string Message) 
{
    lblError.Text = Message; 
    lblError.Visible = True; 
}

Content Page:

Master.ShowMessage("something");
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
ozzijb
  • 674
  • 5
  • 10
  • 17
  • this page has a code for this too http://stackoverflow.com/questions/6332889/call-method-in-master-page –  Jan 08 '15 at 22:43

6 Answers6

19

I think it's a bit late , but for those who are looking for the solution,

Assuming your master page class like:

public MyMAsterPage: MasterPage
{
    public void ShowMessage(string Message) 
    {
       // DO SOMETHING
    }
}

from your content page you can easily call any public method as follow:

(this.Master as MyMasterPage).ShowMessage("Some argument");
Amin
  • 201
  • 2
  • 4
7

Function which define in masterpage:

public void Mesaj(string msj)
{
        lbl_Mesaj.Text = msj;
}

Function which define in content page

protected void Page_Load(object sender, EventArgs e)
{
    MasterPageWeb master = (MasterPageWeb)this.Master;
    master.Mesaj("www.zafercomert.com");
}

You can call masterpage's function from content page like this.

user2245825
  • 71
  • 1
  • 2
2

I ended up just sucking it up and putting the script manager on the master page and putting the label on the master page inside an update panel.

ozzijb
  • 674
  • 5
  • 10
  • 17
1

<%@ MasterType VirtualPath="~/masters/SourcePage.master" %>

Master.Method(); (in code behind)

psychopython
  • 39
  • 1
  • 4
0

Based on Amin's solution, i used an extension method to solve this more often.

public static T GetMasterPageObject<T>(this MasterPage masterPage) where T : MasterPage
{
    return (T)masterPage;
}

Example:

this.Master.GetMasterPageObject<MasterPageClass>().MethodYouNeed("Parameters");
Fabio Martins
  • 55
  • 2
  • 9
0

You'll need to

this.button1 = this.Master.FindControl("UpdatePanel1").FindControl("Button1") as Button;

Here's a blog post to help describe the process. Basically you're drilling down into your controls.

EDIT

Sorry I just re-read your question. The code above will allow you to FIND a Button on your Masterpage from your Content Page codebehind.

It's a little more difficult to execute the Masterpage codebehind method from your content page. A better way might be to a JavaScript event to your button (or just use jQuery), and put the code for a JavaScript modal window in your Masterpage.

<!-- script stuff -->
    <script>
    $(function() {
        $( "#dialog" ).dialog({
            autoOpen: false
        });

        $( "#opener" ).click(function() {
            $( "#dialog" ).dialog( "open" );
            return false;
        });
    });
    </script>
<!-- end script stuff -->

<!-- dialog div -->
    <div id="dialog" title="Basic dialog">
        <p>say something meaningful</p>
    </div>
<!-- end dialog div -->

<!-- content page stuff -->
    <button id="opener">open alert</button>
<!-- end content page stuff-->

HOWEVER

If you're really hooked on calling a masterpage method from your content page, you need to make the method public

Find info in Step 4: Calling the Master Page's Public Members from a Content Page

There are two ways that a content page can programmatically interface with its master page:

  • Using the Page.Master property, which returns a loosely-typed reference to the master page, or
  • Specify the page's master page type or file path via a @MasterType directive; this automatically adds a strongly-typed property to the page named Master.
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • I'm already able to call the method like I noted in my question, the only thing I can't figure out is calling from an update panel. "This works if I don't have an update panel on the content page but is there a way to get it to work when the button is in an update panel?" – ozzijb Oct 21 '10 at 18:04