开发者

Can I bubble up event from Master page to ASPX

开发者 https://www.devze.com 2022-12-18 05:49 出处:网络
Can I bubble up a button click event of a button in mast开发者_运维百科er page to be handled by an event handler in the aspx page ?You can expose the event handler and hookup to it, like this:

Can I bubble up a button click event of a button in mast开发者_运维百科er page to be handled by an event handler in the aspx page ?


You can expose the event handler and hookup to it, like this:

In the master:

public event EventHandler ButtonClick
{
  add { ButtonThatGetsClicked.Click += value; }
  remove { ButtonThatGetsClicked.Click -= value; }
}

In the page:

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);
  ((MyMasterType)Master).ButtonClick += MyHandler;
}

private void MyHandler(object sender, EventArgs e)
{
  //Do Something
}

Also, you can avoid the Master type cast and have it already appear in intellisense as your Master's type by using the @MasterType directive in the aspx markup.


You can rebroadcast the event. Declare a new corresponding event in your master page, such as HelpClicked and then aspx pages that use this master can subscribe to the event and handle it appropriately. The master can also take a default action if there are no subscribers (or use an EventArgs with a Handled property or something like that).

0

精彩评论

暂无评论...
验证码 换一张
取 消