ASP.NET Tip: Use the ItemDataBound Event of a Repeater

My previous ASP.NET tip, "Responding to the Repeater Control's ItemCommand Event", explained how to use a Repeater to respond to events generated within the Repeater. This tip demonstrates how to work with the fields and data within the Repeater.

Each time a data record is added to the Repeater control, an ItemDataBound event is fired. Within the event, you can access the controls that are created, as well as the data being bound to the row. This feature enables you to do a variety of things, such as change the data going in, add attributes to controls, and so forth. This example adds a JavaScript confirmation to the Delete LinkButton control.

The following Repeater control is typical of my own 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>

The first thing to do is to register for ItemDataBound events on your repeater control, which I prefer to do in the OnInit event of the page. Here's the code to do that:

override protected void OnInit(EventArgs e)
{
   base.OnInit(e);
   rptData.ItemDataBound +=
       new RepeaterItemEventHandler(rptData_ItemDataBound);
}

private void rptData_ItemDataBound(object source,
                                   RepeaterCommandEventArgs e)
{

}

Each item will generate an ItemDataBound event, but you will also get events for other types of items, including the header and footer. As a result, you need to protect your code by checking the ItemType before trying to do any work. The following code adds the confirmation dialog:

private void rptData_ItemDataBound(object source,
                                   RepeaterCommandEventArgs e)
{
   if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType !=
      ListItemType.AlternatingItem)
      return;

   LinkButton btn = (LinkButton)e.Item.FindControl("btnDelete");
   btn.Attributes.Add("onclick", "if
      ( ! confirm( 'Delete this record?' )) return false; ");

}

This code looks for Item or AlternatingItem rows, finds the btnDelete control using the FindControl method, and then adds the onclick attribute to the Attributes collection. The result is that when the user clicks the Delete LinkButton, an OK/Cancel dialog pops up to confirm the delete. This is a handy way to protect the user from doing something dangerous.

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.



Comments

  • gfg

    Posted by ddth on 07/18/2014 11:17pm

    Bad Article

    Reply
  • htyyty

    Posted by sadcsdfdfs on 07/09/2013 02:48am

    yujhytjujyutrnbkujiuuj

    Reply
  • Getting Traffic Tactic That Is Also Enabling nike-experts To Rise

    Posted by BobHotgloff on 05/02/2013 05:40pm

    Quick review aids you with most of the workings for nike in addition to what you want to try and do this afternoon. [url=http://www.mizunogoruhujp.com/]ミズノ[/url] The Treasures For nike [url=http://www.mizunogoruhujp.com/ミズノ-ゴルフクラブ-c-1.html]ミズノ mp[/url] Concise study helps you with all the intricacies of the mizuno coupled with the things that you want to do today. [url=http://www.mizunogoruhujp.com/ゴルフグローブ-c-33.html]ミズノプロ グローブ[/url] What other people actually does when considering nike and moreover the actions youshould do different. [url=http://www.mizunogoruhujp.com/ゴルフバッグ-c-7.html]ミズノ[/url] Impartial website shows 5 new stuff concerning nike that none is speaking about. [url=http://www.mizunogoruhu.com/]ミズノ アイアン[/url] A double change on nike [url=http://www.mizunogoruhu.com/ミズノmizuno-クラブ-c-4.html]ミズノ mp[/url] Materials and processing in The Big Apple : nike has left with no good-bye [url=http://www.mizunogoruhu.com/ミズノmizuno-アイアン-c-3.html]ミズノ アイアン[/url] Powerful strategies for nike which you can use starting today. [url=http://www.mizunogoruhu.com/ミズノmizuno-バッグ-c-5.html]ミズノ[/url] Summary statement tells the indisputable details about mizuno and how it may effect your corporation.

    Reply
  • adas

    Posted by tete on 02/21/2013 10:25pm

    asdasd adasd

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds