Microsoft & .NETASPASP.NET Tip: Responding to the Repeater Control's ItemCommand Event

ASP.NET Tip: Responding to the Repeater Control’s ItemCommand Event

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

I’m a bit of an HTML purist, so I love the Repeater control. It allows me to specify exactly which HTML ASP.NET uses to create a table, list, etc. without it adding a bunch of extra junk. The Repeater, like other data-bound controls, contains some events to help you respond to the actions a user performs on controls within the Repeater. This tip shows you how to respond to the ItemCommand event of the Repeater control to perform an action.

The first thing you have to do, once your Repeater control is on your Web page, is register for the ItemCommand event. I prefer to do this within the OnInit event of the page instead of placing it as an attribute of the asp:Repeater tag. Here’s the code, assuming the Repeater is named rptData:

override protected void OnInit(EventArgs e)
{
base.OnInit(e);
rptData.ItemCommand += new RepeaterCommandEventHandler(rptData_ItemCommand);
}

private void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
{

}

The nice thing about this method is that when you type the +=, Visual Studio automatically creates the rptData_ItemCommand handler for you, which is a nice timesaver. Once you have the event handler registered, you can start listening for events.

Here’s an example of a typical Repeater control in my applications:

<asp:Repeater ID=”rptData” Runat=”server”>
<HeaderTemplate>
<p class=”text”><b>Actions:</b>
<asp:LinkButton ID=”btnAdd” Runat=”server” CssClass=”text” CommandName=”add”>Add New Record</asp:LinkButton></p>
<table cellpadding=”4″ cellspacing=”0″ width=”100%”>
<tr class=”tableheading”>
<td width=”80%”>Name</td>
<td width=”20%”>Actions</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class=”tabletext”>
<td class=”tabletext”><%# Eval(“Name”) %></td>
<td align=”center” class=”tabletext”>
<asp:LinkButton ID=”btnEdit” Runat=”server” CssClass=”tabletext” CommandName=”edit”
CommandArgument='<%# Eval(“pkRecordID”) %>’>Edit</asp:LinkButton>
|
<asp:LinkButton ID=”btnDelete” Runat=”server” CssClass=”tabletext” CommandName=”delete”
CommandArgument='<%# Eval(“pkRecordID”) %>’>Delete</asp:LinkButton></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class=”tabletext_gray”>
<td class=”tabletext”><%# Eval(“Name”) %></td>
<td align=”center” class=”tabletext”>
<asp:LinkButton ID=”btnEdit” Runat=”server” CssClass=”tabletext” CommandName=”edit”
CommandArgument='<%# Eval(“pkRecordID”) %>’>Edit</asp:LinkButton>
|
<asp:LinkButton ID=”btnDelete” Runat=”server” CssClass=”tabletext” CommandName=”delete”
CommandArgument='<%# Eval(“pkRecordID”) %>’>Delete</asp:LinkButton></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
If the user clicks the Add button at the top, the ItemCommand routine will fire. The RepeaterCommandEventArgs variable will have a property called CommandName with add in it. If you click Edit or Delete, you’ll get edit or delete in the CommandName and the value of pkRecordID in the CommandArgument variable.

Also note that the Add button is within the Repeater tag. If the button were outside the Repeater tag, it would not generate an ItemCommand. Rather, it would generate a regular Click event. By putting it within the Repeater, I can create a single consolidated routine to handle all actions within the Repeater control.


About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories