当按钮位于更新面板内时,如何从内容页调用母版页方法?
我有一个母版页和一个内容页。在内容页面中,我有一个脚本管理器和一个更新面板。在更新面板中,我希望能够单击一个按钮,该按钮将点击母版页上的公共方法以显示消息。如果我的内容页面上没有更新面板,则此方法有效,但是当按钮位于更新面板中时,有没有办法让它工作?
母版页:
public void ShowMessage(string Message)
{
lblError.Text = Message;
lblError.Visible = True;
}
内容页:
Master.ShowMessage("something");
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为这有点晚了,但是对于那些正在寻找解决方案的人来说,
假设您的母版页类如下:
从您的内容页面您可以轻松地调用任何公共方法,如下所示:
I think it's a bit late , but for those who are looking for the solution,
Assuming your master page class like:
from your content page you can easily call any public method as follow:
母版页中定义的函数:
内容页中定义的函数
您可以像这样从内容页中调用母版页的函数。
Function which define in masterpage:
Function which define in content page
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(); (在后面的代码中)
<%@ MasterType VirtualPath="~/masters/SourcePage.master" %>
Master.Method(); (in code behind)
您需要使用
博客文章来帮助描述该过程。基本上,您正在深入了解您的控件。
编辑
抱歉,我刚刚重新阅读了您的问题。上面的代码将允许您从内容页代码隐藏中查找母版页上的按钮。
从内容页面执行母版页代码隐藏方法有点困难。更好的方法可能是将 JavaScript 事件发送到您的按钮(或者仅使用 jQuery),然后将 JavaScript 代码 模态窗口 在母版页中。
但是,
如果您确实沉迷于从内容页调用母版页方法,则需要将该方法公开
在 步骤 4:从内容页调用母版页的公共成员
You'll need to
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.
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
基于Amin的解决方案,我更经常使用扩展方法来解决这个问题。
例子:
Based on Amin's solution, i used an extension method to solve this more often.
Example: