views:

1740

answers:

1

How do you catch a custom event raised by a master page?

On my master page I have a custom event and delegate:

public event SignOutHandler SignOut;
public delegate void SignOutHandler();

This is raised when a link button on the master page is clicked.

if (SignOut != null)
{
 SignOut();
}

In a user control on the page I'd like to subscribe to that event, but I don't know how I'm supposed to access it. Normally I'd do something like:

MyInstantiatedMasterPage.SignOut += new MyMasterPage.SignOutHandler(MyEvent);

but dealing with a master page means that this isn't possible.

+2  A: 

It's instantiated as a global object, which is what you'll need to use:

((MyMasterPage)Master).SignOut += new MyMasterPage.SignOutHandler(MyEvent);
DannySmurf
I can't seem to access MyMasterPage.SignOutHandler(MyEvent); from within my user control. I can however access it from a web form.
Rob Bell
That's an odd one. If they're in the same namespace, you should be able to access a (non-instance) delegate on a master page from anywhere. Is the control you're trying to access from in the same namespace as master page?
DannySmurf
I have no namespaces in my masters, pages or controls, but they all reside in their own folders. Are namespaces implied by folder names? Regardless, it wouldn't explain why my page can access the master but not my control.
Rob Bell
Adding a reference to the master from my user control helped:<%@ Reference Control="~/Masters/Standard.master" %>
Rob Bell