Sending events from user controls to the parent containers
Right, this is how I prefer to do things, instead of the other solution I wrote about earlier. The scenario is as follows (again):
Imagine having a web form, which has a label control on it and this web form also loads a user control during run-time. The user control has a single button on it and when pressed, the button should set the text value of the label to the current date and time.
This sample lets the user control raise an event, which the parent page catches. The event contains information about what text the label should be set to.
First you define the event argument class so you have a way to send data to the “listener”. This sample is really simple, and contains just a single text property, which will contain a string:
public class MyEventArgs : EventArgs
{
private readonly string text;
public MyEventArgs(string text)
{
this.text = text;
}
public string Text
{
get { return text; }
}
}
Then you go into your user control class and set up the event handler and code to trigger/raise the event with the correct argument values. One could argue if it's really necessary to have the OnEvent() method, but I prefer that, it feels cleaner. I also define the delegate for the event handler in the same class file, you may want to have this in some other place:
public delegate void MyEventHandler(object sender, MyEventArgs e);
public
class WebUserControl1 : System.Web.UI.UserControl{
protected System.Web.UI.WebControls.Button Button1;
public event MyEventHandler MyEvent;
private void Button1_Click(object sender, System.EventArgs e)
{
OnEvent(new MyEventArgs("Time is " + DateTime.Now));
}
protected virtual void OnEvent(MyEventArgs e)
{
if (MyEvent != null)
MyEvent(this,e);
}
}
Now, the rest of the code goes into the web form class, where you have to add the event listener after you’ve loaded the control. The Page_Load() event loads the control and sets up the event handler for MyEvent, called ctrl_MyEvent(). When the event fires, you just extract the text property from the event arguments and set the label text to that value.
It’s a bit harder if you need to load different kinds of controls dynamically, but in that case I would define the events in a superclass that all user controls inherit from:
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Label Label1;
protected WebUserControl1 ctrl = null;
private void Page_Load(object sender, System.EventArgs e)
{
ctrl = (WebUserControl1)LoadControl("WebUserControl1.ascx");
Panel1.Controls.Add(ctrl);
ctrl.MyEvent +=new MyEventHandler(ctrl_MyEvent);
}
private void ctrl_MyEvent(object sender, MyEventArgs e)
{
Label1.Text = e.Text;
}
}