views:

28

answers:

2

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");
A: 

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.
rockinthesixstring
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?"
salietata
A: 

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.

salietata