Handling User Control Events in ASP.Net
Join the DZone community and get the full member experience.
Join For FreeAlthough most developers know how to handle events of User Controls, this will help for those developers who have never come across this before and wants to learn with a simple example the details about how to handle custom user control events on aspx pages.
What are User Controls?
User controls are just like standard ASP.Net webform with few limitations. It contains a designer view where the user can drag and drop existing controls of ASP.net or third party to structure that user control. Inorder to learn more about User control you can follow this link http://msdn.microsoft.com/en-us/library/y6wb1a0e(v=vs.100).aspx
How to Handling custom developed User Control Event
Let suppose we have a user control containing a textbox and a button. And we have an aspx page where we are using that control. Now inorder to handle the button click event of the User control's button click event on aspx page we will go through the following steps.
Following is a code snippet of User Control named "ctrlCustomUserControl.ascx"
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ctrlMyUserControl.ascx.cs" Inherits="WebApplication7.ctrlMyUserControl" %> <asp:TextBox ID="txtBox" runat="server"></asp:TextBox> <asp:Button ID="btnClickMe" runat="server" Text="Click Me!" onclick="btnClickMe_Click" />
You can see there are two controls placed, one is textbox and the other is button.
Code behind file looks like as follows
public partial class ctrlMyUserControl : System.Web.UI.UserControl { public delegate void ClickHandler(object sender, EventArgs e); public event ClickHandler Click; protected void btnClickMe_Click(object sender, EventArgs e) { Click(sender, e); } public String Text { set { this.txtBox.Text = value; } } }
Above code shows that there is a delegate named ClickHandler and I have declare its event named Click. And I have also registered the button click event and called my custom Click event there, which will be registered by the ASPX page will be invoked from this event.
Now, lets look into the "Default.aspx" page where I am using this control.
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication7._Default" %> <%@ Register src="~/ctrlMyUserControl.ascx" TagName="MyControl" TagPrefix="uc"%> <form runat="server"> <uc:MyControl id="myctrl" runat="server" OnClick="myctrl_click"></uc:MyControl> </form>
Second statement shows the registration of my custom user control on aspx page, tagprefix and tagname values are used to define control tag.
I have used uc as the tagprefix and MyControl as tagname. You can see how I have specified the markup of custom user control. One important thing, you can see that I have specified OnClick event. Actually if you recall the control code behind file there I have an event named "Click" and inorder to register it on any aspx page we have to add a prefix "On" i.e. "OnClick". This is how we can register custom events of user the control on aspx page.
Code behind of Default.aspx is as follows
public partial class _Default : System.Web.UI.Page { protected void myctrl_click(object sender, EventArgs e) { myctrl.Text = "Hello World!"; } }
When user clicks on the button, it will populate "Hello World!" text on the text box.
Happy Coding!
Opinions expressed by DZone contributors are their own.
Comments