How do you call master page methods from a content page when the button is inside an update panel?
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 ShowMes开发者_运维百科sage(string Message)
{
lblError.Text = Message;
lblError.Visible = True;
}
Content Page:
Master.ShowMessage("something");
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");
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.
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.
<%@ MasterType VirtualPath="~/masters/SourcePage.master" %>
Master.Method(); (in code behind)
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.
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");
精彩评论