October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

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

  • May 29, 2006
  • By Eric Smith
  • Send Email »
  • More Articles »

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.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel