Handling user control events on the parent page
January 23, 2009A user control operates virtually the same as a standard aspx page does, with it's own controls, events and lifecycle. Many times, however, you'll need to know that a certain event occurred in the user control so the parent page that holds it can act appropriately.
You can expose and handle controls with their own events, such as a button click, but you can also create your own events and even include a custom EventArgs object that can contain any information you need to pass up to the parent page.
Let's create a simple user control with a couple buttons and a text box. The first button we'll use to demonstrate capturing it's click event, and the second button we'll use to fire our own event which will pass text value contained in the text box.
The User Control (aspx):
<asp:Button ID="Button1" runat="server" Text="Button Event" onclick="Button1_Click" /><br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" Text="Custom Event" onclick="Button2_Click" />
The User Control (code-behind):
public event EventHandler MyEvent;
protected void OnMyEvent(EventArgs e)
{
if (MyEvent != null)
MyEvent(this, e);
}
protected void Button1_Click(object sender, EventArgs e)
{}
protected void Button2_Click(object sender, EventArgs e)
{
MyEventArgs mea = new MyEventArgs();
mea.Message = TextBox1.Text.Trim();
OnMyEvent(mea);
}
As you can see above, we’ve created an event handler, an event, and in the Button2_Click event, we have instantiated a custom EventArgs object (see class below) and raised the event. You can use use the EventArgs (e) object if you wish, but in this case I’m using my own custom class which inherits EventArgs, and have exposed a property from which I can include the text value of TextBox1.
The custom EventArgs class:
public class MyEventArgs : EventArgs
{
public MyEventArgs()
{}
public string Message { get; set; }
}
And now let’s see the parent page (code-behind):
protected override void OnInit(EventArgs e)
{
ucMyControl.MyEvent += new EventHandler(ucMyControl_MyEvent);
Button button = (Button)ucMyControl.FindControl("Button1");
button.Click += new EventHandler(button_Click);
base.OnInit(e);
}
void button_Click(object sender, EventArgs e)
{
Label1.Text = "The \"Button Event\" button in the user control was clicked!";
}
void ucMyControl_MyEvent(object sender, EventArgs e)
{
Label1.Text = "The custom event was fired, with the message: " +
((MyEventArgs)e).Message;
}
We’ve overridden the OnInit event and added two event handlers: the Click event of Button1 and the custom “My Event” which is raised during the click event of Button2 inside the user control.
To add the custom event, simply reference the event name, type “+=” (plus equals) and then Tab and Visual Studio will add the event for you. The Button click event requires one more step – you’ll first have to create an instance of the button object using FindControl() on the user control and reference its Click event.
Tags: user control, event handling, eventargs
Categories: ASP.NET, C#
