简体   繁体   English

如何在MasterPage中有一个按钮并在内容页面中以不同的方式使用

[英]How to have a button in MasterPage and use differently in content pages

I have a button in my MasterPage: 我的MasterPage中有一个按钮:

<asp:Button ID="tyt" runat="server" ClientIDMode="Static" OnClick="tyt_Click" UseSubmitBehavior="false" Text="SUBMIT A MESSAGEBOX" />

I want to use the button to do different thing in different pages. 我想使用该按钮在不同页面中执行不同的操作。

I added the following in my MasterPage: 我在MasterPage中添加了以下内容:

public void tyt_Click(object sender, EventArgs e)
        {
            OnMasterControlClick(e);
        }
        protected void OnMasterControlClick(EventArgs e)
        {
            if (this.MasterControlClicked != null)
            {
                this.MasterControlClicked(this, e);
            }
        }

The following in page1.aspx: page1.aspx中的以下内容:

protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (this.Master is Site)
            {
                ((Site)this.Master).MasterControlClicked += new EventHandler(ContentControlClicked);
            }
        }
        private void ContentControlClicked(object sender, EventArgs e)
        {
            MessageBox.Show("your task");
        }

The following in page2.aspx: page2.aspx中的以下内容:

protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (this.Master is Site)
            {
                ((Site)this.Master).MasterControlClicked += new EventHandler(ContentControlClicked);
            }
        }
        private void ContentControlClicked(object sender, EventArgs e)
        {
            MessageBox.Show("your task");
        }

Only Your task is displayed if I am on page1.aspx but when I am on page2.aspx and I press the button, the page just reloads. 如果我在page1.aspx上,则仅显示“ Your task ,但是当我在page2.aspx上并按该按钮时,页面将重新加载。

How do I fix it? 我如何解决它?

This can be solved using an interface. 可以使用界面来解决。 Create an interface that defines the action or actions you need. 创建一个定义所需动作的界面。

public interface IActionPage
{
    void Action();
}

Then have each of your pages implement the interface. 然后让您的每个页面都实现该接口。

public partial class _Default : System.Web.UI.Page, IActionPage
{ 
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void Action()
    {
        MessageBox.Show("your task");
    }
}

Finally, have the master page's button cast the current page to the interface and then invoke the method. 最后,让母版页的按钮将当前页投射到界面上,然后调用该方法。

protected void tyt_Click(object sender, EventArgs e)
{
    var ActionPage = this.Page as IActionPage;
    if (ActionPage != null)
        ActionPage.Action();
}

another way of adding event handler is using delegate Action<> or Func<> for handler 添加事件处理程序的另一种方法是对处理程序使用委托Action <>或Func <>

in Master page. 在母版页中。

public Action MyButtonCallBack {get; set;}

in Child page. 在子页面中。

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    if (this.Master is Site)
    {
        ((Site)this.Master).MyButtonCallBack = DoSomething;
    }
}

private void DoSomething() 
{
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM