C# Event Handlers and Delegates in ASP .Net with Web User Controls
This article should help as a general how to on event handlers and delegates in C# as well as propose a different way to handle cross page methods in your ASP .Net website from a web user control or other page.
Dilemma:
Page contains user control which has some methods and functionality built in. When a particular button click or other generic event is fired in the user control, it needs to call back to the parent page.
Solution:
Create event handlers and their delegates in the user control, fire them from the methods tied to the internal controls protected events, and define the methods that will handle the new events in the page (see code below).
C# (with some VB notes):
//delegate declaration not necessary in VB public delegate void MyCustomHandler(object sender, EventArgs e); public event MyCustomHandler SomethingClicked; protected void btnButton1_Click(object sender, EventArgs e) { //tell our parent page which is listening for the event that something was clicked //this null check and method invoke is the equivalent of raise event in VB if (SomethingClicked != null) { SomethingClicked(sender, e); } //do some other work specific to this button in the user control }
That’s the code for your user control, now for the page.
protected void Page_Load(object sender, EventArgs e) { MyUserControl1.SomethingClicked += new MyUserControl1.MyCustomHandler(MyUserControl1_SomethingClicked); if (!Page.IsPostBack) { //do my other normal work } } protected void MyUserControl1_SomethingClicked(object sender, EventArgs e) { //voila! clicking on the button in your user control will fire this method on the parent page! }
Alternatively instead of declaring your own delegate, you can also simply use:
//in control public event EventHandler SomethingClicked; //and in page load MyUserControl1.SomethingClicked+= new EventHandler(MyUserControl1_SomethingClicked);
If you have no need for custom arguments, this a good quick alternative that may be well liked by VB users where delegate declaration is optional.
References
DeveloperFusion, http://www.developerfusion.com/article/2137/event-handling-in-net-using-c/3/
VBForums, http://www.vbforums.com/showthread.php?t=521089
MSDN (events and delegates), http://msdn.microsoft.com/en-us/library/17sde2xt%28v=vs.71%29.aspx
MSDN (dynamically bind event handlers), http://msdn.microsoft.com/en-us/library/t3d01ft1%28v=vs.80%29.aspx
MSDN (which control raised an event), http://msdn.microsoft.com/en-us/library/zk6b17bs%28v=vs.80%29.aspx
ASP.Net (blog), http://weblogs.asp.net/rweigelt/archive/2005/01/14/353333.aspx
TechRepublic, http://www.techrepublic.com/article/simplify-net-class-communication-with-delegates/1050214
Akadia, http://www.akadia.com/services/dotnet_delegates_and_events.html
Posted on August 9, 2011, in Programming & Development and tagged asp.net, c#, controls, csharp, delegate, event handler, user control, web, web user control. Bookmark the permalink. Leave a comment.
Leave a comment
Comments 0