开发者

Can I bubble up event from Master page to ASPX

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜