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

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

问题描述

关于同一问题的问题和文章不止于此,但我还有几个相关的问题,希望能得到一些答案.

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. 我听说有两种方法可以找到按钮并添加处理程序或使用接口(从 这里) .. 你推荐哪一个?

  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 ?

如果您能用一些代码说明接口"选项以及接口文件的分类位置,因为当我尝试继承它时,它在页面中不可读!

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.

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

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