Wednesday, March 26, 2008

Subscribing to Events in Master Pages

There has been a lot written on communicating between master pages and user controls and some of it I actually understood. However, I never could get one solution to work for me so I ended up taking the approaches of a few and then making up the rest. What I ended up with was a workable solution.

My problem:
I have a master page with a list of projects and a button to view those projects. I have a nested master page within the main master page, then a page, and finally a user control.

I need to capture the event in my user control when the button on the main master page has been clicked.

Solution:
What I ended up doing was creating an interface called ISiteMaster. In this interface I define the event I want to subscribe to.

public interface ISiteMaster
{
event CommandEventHandler ViewProjects;
}

I implement the interface from the main master page. In the button event on my master page I then raise this event.
On the user control I then subscribe to this event and assign it to a method.

So now when I click the button on my master page it gets captured in the user control and voila!, the method on my user control is called.

0 comments: