NCS Logo - Click for home page Northstar Developer Center
Platforms
All Platforms
.NET Framework (1.x - 4.x)
Active Server Pages
ASP.NET
C#
SQL Server
VB.NET
Visual Basic

Keywords
.NET Data Types
.NET E-mail
.NET Events
.NET Functions
.NET Object Programming
.NET System.Configuration
.NET System.Diagnostics
.NET System.IO
.NET System.Net
.NET System.Net.Sockets
Active Data Objects
ASP Architecture
ASP Black Belt
ASP Built-in Functions
ASP Built-in Objects
ASP Debugging
ASP Performance
ASP Security
ASP Syntax
ASP.NET Authentication
ASP.NET Controls
ASP.NET Data Access
ASP.NET Features
ASP.NET Master Pages
ASP.NET Page Events
ASP.NET Security
ASP.NET ViewState
Atom
Certifications
COM, DCOM, COM+
Data Access
E-Mail
Errors
Exporting Data
HTML Tips
IIS
Object-Oriented Programming
RSS
SQL
Uncategorized ASP Tips
VB API Programming
VB Forms
VB Syntax
XML

Book Support
Visual Basic 6 Bible
ASP Bible
ASP Weekend Crash Course
ASP.NET At Work
Creating Web Services

Using the ItemDataBound Event of the Repeater

Written by Eric Smith, Northstar Computer Systems LLC

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, etc. 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">Actions: 
    <asp:LinkButton ID="btnAdd" Runat="server" CssClass="text" CommandName="add">Add New         Record</asp:LinkButton>

<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.

Keywords: [ ASP.NET Controls ]

Publication Date: 5/31/2006, Last Update: 12/10/2010