Hi Tevin,
Repeater has ItemDataBound event same as Gridview RowDataBound event.
But ItemDataBound occurs after an item in the Repeater control is data bound but before it is rendered on the page.
So to implement this you have two option.
1. Using ItemCreated event.
2. Using protected method.
I have created a sample please take its reference.
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
1. Using ItemCreated event
HTML
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated">
<HeaderTemplate>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "Id")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Status")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
dt.Rows.Add(1, "John Hammond", "True");
dt.Rows.Add(2, "Mudassar Khan", "False");
dt.Rows.Add(3, "Suzanne Mathews", "True");
dt.Rows.Add(4, "Robert Schidner", "False");
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
}
}
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RepeaterItem item = e.Item;
DataRow dr = (item.DataItem as DataRowView).Row;
string status = dr["Status"].ToString();
if (status.ToLower().Trim() == "true")
{
dr["Status"] = "Abnormal";
}
else
{
dr["Status"] = "Normal";
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("Id", GetType(String)))
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Status", GetType(String)))
dt.Rows.Add(1, "John Hammond", "True")
dt.Rows.Add(2, "Mudassar Khan", "False")
dt.Rows.Add(3, "Suzanne Mathews", "True")
dt.Rows.Add(4, "Robert Schidner", "False")
Me.Repeater1.DataSource = dt
Me.Repeater1.DataBind()
End If
End Sub
Protected Sub Repeater1_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim item As RepeaterItem = e.Item
Dim dr As DataRow = (TryCast(item.DataItem, DataRowView)).Row
Dim status As String = dr("Status").ToString()
If status.ToLower().Trim() = "true" Then
dr("Status") = "Abnormal"
Else
dr("Status") = "Normal"
End If
End If
End Sub
2. Using protected method
HTML
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>Id</th>
<th>Name</th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "Id")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
<td><%# SetText(DataBinder.Eval(Container.DataItem, "Status").ToString()) %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
dt.Rows.Add(1, "John Hammond", "True");
dt.Rows.Add(2, "Mudassar Khan", "False");
dt.Rows.Add(3, "Suzanne Mathews", "True");
dt.Rows.Add(4, "Robert Schidner", "False");
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
}
}
protected string SetText(string item)
{
if (item.ToLower().Trim() == "true")
{
return "Abnormal";
}
else
{
return "Normal";
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("Id", GetType(String)))
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Status", GetType(String)))
dt.Rows.Add(1, "John Hammond", "True")
dt.Rows.Add(2, "Mudassar Khan", "False")
dt.Rows.Add(3, "Suzanne Mathews", "True")
dt.Rows.Add(4, "Robert Schidner", "False")
Me.Repeater1.DataSource = dt
Me.Repeater1.DataBind()
End If
End Sub
Protected Function SetText(ByVal item As String) As String
If item.ToLower().Trim() = "true" Then
Return "Abnormal"
Else
Return "Normal"
End If
End Function
Screenshot