How to handle master page button event in content page?(如何处理内容页面中的母版页按钮事件?)
问题描述
关于同一问题的问题和文章不止于此,但我还有几个相关的问题,希望能得到一些答案.
There's more than question and article about the same exact question but I have a couple more related questions and was hoping to get some answers.
我听说有两种方法可以找到按钮并添加处理程序或使用接口(从 这里) .. 你推荐哪一个?
I've heard of two approaches to find the button and add the handler or use an interface (Check both approaches from here) .. Which one do you suggest ?
如果您能用一些代码说明接口"选项以及接口文件的分类位置,因为当我尝试继承它时,它在页面中不可读!
If you could please illustrate the 'Interface' option with some code and where to class the interface file cause it's not readable in the page when I try to inherit it!
推荐答案
第二种方法是 IMO 更好.第一种选择将一个页面与特定的母版页耦合,这并不好.
Second aproach is IMO better. The first choice couples a page to the specific master page, and it is not nice.
所有文件都放在同一个文件夹中.
All files are placed in the same folder.
IPageInterface.cs:
IPageInterface.cs:
namespace CallFromMasterPage
{
public interface IPageInterface
{
void DoSomeAction();
}
}
默认.aspx.cs:
Default.aspx.cs:
namespace CallFromMasterPage
{
public partial class Default : System.Web.UI.Page, IPageInterface
{
public void DoSomeAction()
{
throw new NotImplementedException();
}
}
}
Site.Master.cs:
Site.Master.cs:
namespace CallFromMasterPage
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Button1_Click(object sender, EventArgs e)
{
IPageInterface pageInterface = Page as IPageInterface;
if (pageInterface != null)
{
pageInterface.DoSomeAction();
}
}
}
}
还有其他方法.例如.您可以通过事件代理发布事件.
There are other approaches. E.g. you can publish an event via event broker.
这篇关于如何处理内容页面中的母版页按钮事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何处理内容页面中的母版页按钮事件?
基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
