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.
这篇关于如何处理内容页面中的母版页按钮事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何处理内容页面中的母版页按钮事件?
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 使用 rss + c# 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
