posted 6/22/2011 by Raghav Khunger
I was working with my custom repeater control today. In it's header template I was having a ASP.NET Button control. I had to capture the click of this button control. There were other controls too for which also I needed to bind events. In this blog I will explain the example with button's click only. Below is the code: ASPX Code:
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" %> <%@ Register TagPrefix="MyControls" Namespace="MyControls" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="HtmlForm" runat="server"> <div> <MyControls:MyRepeater ID="MyRepeater1" runat="server"> <HeaderTemplate> <asp:Button ID="MyButton" runat="server" Text="Click Me" /> </HeaderTemplate> <ItemTemplate> </ItemTemplate> </MyControls:MyRepeater> </div> </form> </body> </html>
Now let's come to server side code i.e the code of repeater.
CustomControl code (Custom Repeater) MyRepeater.cs
using System; using System.Collections.Generic; using System.Drawing; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MyControls { [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), ToolboxBitmap(typeof(Repeater)), ToolboxData("<{0}:MyRepeater runat=\"server\" />")] public class MyRepeater : Repeater{ /// <summary> /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event. /// </summary> /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param> protected override void OnInit(EventArgs e) { ItemDataBound += MyRepeater_ItemDataBound; ItemCommand += MyRepeater_ItemCommand; } /// <summary> /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event and performs other initialization. /// </summary> /// <param name="e">The <see cref="T:System.Web.UI.WebControls.RepeaterItemEventArgs"/> object that contains the event data.</param> protected override void OnLoad(EventArgs e) { if (!Page.IsPostBack) { //Dummy binding of repeater var list = new List<int> {1, 2}; this.DataSource = list; this.DataBind(); } } /// <summary> /// Handles the ItemDataBound event of the MyRepeater control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs"/> instance containing the event data.</param> protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { var myButton = (Button)e.Item.FindControl("MyButton"); myButton.CommandName = "MyButtonCommand"; } } /// <summary> /// Handles the ItemCommand event of the MyRepeater control. /// </summary> /// <param name="source">The source of the event.</param> /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param> protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "MyButtonCommand") { //Do your work } } } }
Above in the ItemDatabound method of the repeater button's control CommandName property is set. Now when the button is clicked the ItemCommand method is called and with the help of CommandName property you can judge what is the command name and then you can do your work with it. One thing which is to be keep in mind above, this.DataBind(); should be inside !Page.IsPostback if loop else the repeater will be rebinded.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18