如何处理在内容页母版页按钮事件? [英] How to handle master page button event in content page?

查看:420
本文介绍了如何处理在内容页母版页按钮事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有比约完全相同的问题,问题和文章,但我有一对夫妇更多相关的问题,并希望能得到一些答案。




  1. 我听说过两种方法来查找按钮,并添加处理程序,或使用一个接口(从检查的这里)..你认为哪一个?


  2. 如果你能请说明接口选项一些代码并在类接口文件因为它不是在页面读取当我尝试继承吧!



解决方案

二是形式给出更好的IMO。第一种选择的夫妻一个页面的特定母版页,这是不是很好。



所有的文件都放在同一个文件夹中。



IPageInterface.cs:

 命名空间CallFromMasterPage 
{
公共接口IPageInterface
{
无效DoSomeAction();
}
}



Default.aspx.cs:

 命名空间CallFromMasterPage 
{
公共部分类默认:System.Web.UI.Page,IPageInterface
$ { b $ b酒店的公共空间DoSomeAction()
{
抛出新NotImplementedException();
}
}
}



Site.Master.cs:

 命名空间CallFromMasterPage 
{
公共部分类SITEMASTER:System.Web.UI.MasterPage
{
保护无效的button1_Click(对象发件人,EventArgs五)
{
IPageInterface pageInterface =页面为IPageInterface;
如果(pageInterface!= NULL)
{
pageInterface.DoSomeAction();
}
}
}
}

有其他的方法。例如。您可以通过发布事件代理的事件。


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.

  1. 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 ?

  2. 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!

解决方案

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:

namespace CallFromMasterPage
{
    public interface IPageInterface
    {
        void DoSomeAction();
    }
}

Default.aspx.cs:

namespace CallFromMasterPage
{
    public partial class Default : System.Web.UI.Page, IPageInterface
    {
        public void DoSomeAction()
        {
            throw new NotImplementedException();
        }
    }
}

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.

这篇关于如何处理在内容页母版页按钮事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆