Events and Delegates with ASP.NET master pages
-
03-07-2019 - |
Question
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.
Solution
It's instantiated as a global object, which is what you'll need to use:
((MyMasterPage)Master).SignOut += new MyMasterPage.SignOutHandler(MyEvent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow