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");
5 个解决方案
#1
16
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");
#2
6
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.
你可以从这样的内容页面调用masterpage的功能。
#3
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.
我最终只是将它吮吸并将脚本管理器放在母版页上并将标签放在更新面板内的母版页上。
#4
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.
从内容页面执行Masterpage代码隐藏方法要困难一些。更好的方法可能是对您的按钮的JavaScript事件(或只是使用jQuery),并将JavaScript模式窗口的代码放在您的母版页中。
<!-- 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
在步骤4:从内容页面调用主页面的公共成员中查找信息
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
- 使用Page.Master属性,该属性返回对母版页的松散类型引用,或
- 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.
- 通过@MasterType指令指定页面的主页类型或文件路径;这会自动将强类型属性添加到名为Master的页面。
#5
0
Based on Amin's solution, i used an extension method to solve this more often.
基于Amin的解决方案,我使用扩展方法来更频繁地解决这个问题。
public static T GetMasterPageObject<T>(this MasterPage masterPage) where T : MasterPage
{
return (T)masterPage;
}
Example:
例:
this.Master.GetMasterPageObject<MasterPageClass>().MethodYouNeed("Parameters");
#1
16
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");
#2
6
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.
你可以从这样的内容页面调用masterpage的功能。
#3
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.
我最终只是将它吮吸并将脚本管理器放在母版页上并将标签放在更新面板内的母版页上。
#4
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.
从内容页面执行Masterpage代码隐藏方法要困难一些。更好的方法可能是对您的按钮的JavaScript事件(或只是使用jQuery),并将JavaScript模式窗口的代码放在您的母版页中。
<!-- 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
在步骤4:从内容页面调用主页面的公共成员中查找信息
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
- 使用Page.Master属性,该属性返回对母版页的松散类型引用,或
- 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.
- 通过@MasterType指令指定页面的主页类型或文件路径;这会自动将强类型属性添加到名为Master的页面。
#5
0
Based on Amin's solution, i used an extension method to solve this more often.
基于Amin的解决方案,我使用扩展方法来更频繁地解决这个问题。
public static T GetMasterPageObject<T>(this MasterPage masterPage) where T : MasterPage
{
return (T)masterPage;
}
Example:
例:
this.Master.GetMasterPageObject<MasterPageClass>().MethodYouNeed("Parameters");