In this article I will explain with an example, how to use IF ELSE condition in ItemTemplate of Repeater in ASP.Net using C# and VB.Net.
IF ELSE statement is not supported in ItemTemplate of ASP.Net Repeater, 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.
 
 
Using IF ELSE condition in ItemTemplate of Repeater in ASP.Net
The following HTML Markup consists of an ASP.Net Repeater control with an HTML Table Layout.
Note: For more information on displaying HTML Table Layout using Repeater control, please refer Repeater control Tutorial with example in ASP.Net using C# and VB.Net.
 
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="Repeater1" 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="Repeater1" 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#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net Repeater control
The Repeater is populated with a dynamic DataTable with some dummy data inside the Page Load event handler.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
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");
        Repeater1.DataSource = dt;
        Repeater1.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")
        Repeater1.DataSource = dt
        Repeater1.DataBind()
    End If
End Sub
 
 
Screenshot
Using IF ELSE condition in ItemTemplate of Repeater in ASP.Net
 
 
Demo
 
 
Downloads