且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何处理内容页面中的母版页按钮事件?

更新时间:2023-02-02 19:47:56

第二种方法是 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.