In this article I will explain with an example, how to use the ItemDataBound (OnItemDataBound) event of Repeater in ASP.Net using C# and VB.Net.
ItemDataBound event is triggered for each Repeater Item when it is bound to the Data row.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater control with an HTML Table Layout.
<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound = "OnItemDataBound">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
CustomerId
</th>
<th scope="col" style="width: 120px">
Name
</th>
<th scope="col" style="width: 100px">
Country
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
</td>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Populating Repeater control from database in ASP.Net
Inside the Page load event handler, the Repeater is populated with the records from the Customers table.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindRepeater();
}
}
private void BindRepeater()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rptCustomers.DataSource = dt;
rptCustomers.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindRepeater()
End If
End Sub
Private Sub BindRepeater()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
rptCustomers.DataSource = dt
rptCustomers.DataBind()
End Using
End Using
End Using
End Sub
The Repeater ItemDataBound event handler
Following is the ItemDataBound event handler of the Repeater control. First a check is made to verify whether the item is of Repeater Item and Alternating Item and not Header or Footer Item.
Then the Repeater item is referenced using the Item property of the RepeaterItemEventArgs object.
The values of the controls inside the Repeater item are fetched by referencing the controls using FindControl method.
C#
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Reference the Repeater Item.
RepeaterItem item = e.Item;
//Reference the Controls.
string customerId = (item.FindControl("lblCustomerId") as Label).Text;
string name = (item.FindControl("lblName") as Label).Text;
string country = (item.FindControl("lblCountry") as Label).Text;
}
}
VB.Net
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
'Reference the Repeater Item.
Dim item As RepeaterItem = e.Item
'Reference the Controls.
Dim customerId As String = (TryCast(item.FindControl("lblCustomerId"), Label)).Text
Dim name As String = (TryCast(item.FindControl("lblName"), Label)).Text
Dim country As String = (TryCast(item.FindControl("lblCountry"), Label)).Text
End If
End Sub
Screenshot
Downloads