In this article I will explain with an example, how to use IF ELSE condition in ItemTemplate of Repeater control in ASP.Net using C# and VB.Net.
IF ELSE statement is not supported in ItemTemplate of ASP.Net Repeater control, but using Inline Expressions and Ternary Operator with the EVAL function, a simple IF ELSE condition can be easily implemented.
Concept
In database tables, we have some specific codes which have specific meaning. Programmers are aware of the significance of these codes but end users won’t understand what a particular code means. In such case we will have to dynamically change the code value to some meaningful and easy to understand words.
For example, consider a scenario of Meeting attendance. The Status column in database consists of two values A and P where A means Absent i.e. the person has not attended meeting and P means Present i.e. the person has attended the meeting.
Now end users won’t understand what is A and P and hence by making use of IF ELSE condition with EVAL function, A will be replaced with Absent and P will be replaced with Present which now makes it more meaningful.
HTML Markup
The HTML Markup consists of following controls:
Repeater – For displaying data.
The Repeater control consists of following templates:–
HeaderTemplate – The content of this template will not be repeated and will be placed in the top most position i.e. head section of the Repeater control.
ItemTemplate – The content of this template will be repeated for each record present in its DataSource.
ItemTemplate consists of three Label controls.
Label – For displaying the columns value.
FooterTemplate – The content of this template will not be repeated and will be placed in the bottom most position i.e. footer section of the Repeater control.
Using Inline Expression, the value of the EVAL function is compared and if the Status value is A then the string Absent is displayed else present.
Note: The syntax of using EVAL function within Inline Expression is different for C# and VB.Net.
C#
<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 30px">Id</th>
<th scope="col" style="width: 120px">Name</th>
<th scope="col" style="width: 100px">Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" Text='<%# Eval("Id") %>' /></td>
<td><asp:Label runat="server" Text='<%# Eval("Name") %>' /></td>
<td><asp:Label runat="server" Text='<%# Eval("Status").ToString() == "A" ? "Absent" : "Present" %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
VB.Net
<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 30px">Id</th>
<th scope="col" style="width: 120px">Name</th>
<th scope="col" style="width: 100px">Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" Text='<%# Eval("Id") %>' /></td>
<td><asp:Label runat="server" Text='<%# Eval("Name") %>' /></td>
<td><asp:Label runat="server" Text='<%# If(Eval("Status").ToString() = "A", "Absent", "Present") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net Repeater control
Inside the
Page_Load event handler, the dynamic
DataTable is created with dummy records.
Finally, the
DataTable is assigned to the DataSource property of Repeater control and Repeater control is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Status") });
dt.Rows.Add(1, "John Hammond", "A");
dt.Rows.Add(2, "Mudassar Khan", "P");
dt.Rows.Add(3, "Suzanne Mathews", "P");
dt.Rows.Add(4, "Robert Schidner", "A");
rptCustomers.DataSource = dt;
rptCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Status")})
dt.Rows.Add(1, "John Hammond", "A")
dt.Rows.Add(2, "Mudassar Khan", "P")
dt.Rows.Add(3, "Suzanne Mathews", "P")
dt.Rows.Add(4, "Robert Schidner", "A")
rptCustomers.DataSource = dt
rptCustomers.DataBind()
End If
End Sub
Screenshot
Demo
Downloads