views:

181

answers:

3

Can I bubble up a button click event of a button in master page to be handled by an event handler in the aspx page ?

A: 

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).

Sam
+1  A: 

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.

Nick Craver