Using conditionals in ASP.NET data binding expressions

ASP.NET 2.0 has no support for using conditionals in data binding expressions but it will change in ASP.NET 4.0. In this posting I will show you how to implement Iif() function for ASP.NET 2.0 and how ASP.NET 4.0 solves this problem smoothly without any code.

Problem

Let’s say we have simple repeater.

<asp:Repeater runat="server" ID="itemsList">
    <HeaderTemplate>
        <table border="1" cellspacing="0" cellpadding="5">
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
        <td align="right"><%# Container.ItemIndex + 1 %>.</td>
        <td><%# Eval("Title") %></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate
>
</
asp:Repeater
>

Repeater is bound to data when form loads.

protected void Page_Load(object sender, EventArgs e)
{
   
var items = new
[] {
           
new { Id = 1, Title = "Headline 1"
},
           
new { Id = 2, Title = "Headline 2"
},
           
new { Id = 2, Title = "Headline 3"
},
           
new { Id = 2, Title = "Headline 4"
},
           
new { Id = 2, Title = "Headline 5" }
        };
    itemsList.DataSource = items;
    itemsList.DataBind();
}

Color tableWe need to format even and odd rows differently. Let’s say we want even rows to be with whitesmoke background and odd rows with white background. Just like shown on screenshot on right.

Our first thought is to use some simple expression to avoid writing custom methods. We cannot use construct like this

<%# Container.ItemIndex % 2==0 ? "white" : "whitesmoke"  %>

because all we get are template compilation errors.

ASP.NET 2.0: Iif() method

For ASP.NET 2.0 pages and controls we can create Iif() method and call it from our templates. This is out Iif() method.

protected object Iif(bool condition, object trueResult, object falseResult)
{
   
return condition ? trueResult : falseResult;
}

And here you can see how to use it.

<asp:Repeater runat="server" ID="itemsList">
  <HeaderTemplate>
    <table border="1" cellspacing="0" cellpadding="5">
    </HeaderTemplate>
  <ItemTemplate>
    <tr style='background-color:'
      <%# Iif(Container.ItemIndex % 2==0, "white", "whitesmoke") %>'>
      <td align="right">
        <%# Container.ItemIndex + 1 %>.</td>
      <td>
        <%# Eval("Title") %></td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate
>
</
asp:Repeater
>

This method does not care about types because it works with all objects (and value-types). I had to define this method in code-behind file of my user control because using this method as extension method made it undetectable for ASP.NET template engine.

ASP.NET 4.0: Conditionals are supported

In ASP.NET 4.0 we will write … hmm … we will write nothing special. Here is solution.

<asp:Repeater runat="server" ID="itemsList">
  <HeaderTemplate>
    <table border="1" cellspacing="0" cellpadding="5">
    </HeaderTemplate>
  <ItemTemplate>
    <tr style='background-color:'
      <%# Container.ItemIndex % 2==0 ? "white" : "whitesmoke" %>'>
      <td align="right">
        <%# Container.ItemIndex + 1 %>.</td>
      <td>
        <%# Eval("Title") %></td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate
>
</
asp:Repeater
>

Yes, it works well. :)

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    6 thoughts on “Using conditionals in ASP.NET data binding expressions

    • April 5, 2010 at 6:38 am
      Permalink

      When you tried to use the extension method, did you imported the namespace of the extension method class (through web.config or by using control declarations) and used this keyword to call it?

    • April 5, 2010 at 7:53 am
      Permalink

      You can do:
      <%# Container.ItemIndex % 2==0 ? "white" : "whitesmoke" % today without need to wait for ASP.NET 4.0. I think it's supported since .NET 1.0

    • April 10, 2010 at 2:40 am
      Permalink

      Mohamed, you think it’s supported but have you actually tried it? :)

    • April 14, 2010 at 5:18 pm
      Permalink

      I just use IIF with the EVAL function in ASP.NET 2.0

    • October 12, 2011 at 2:52 pm
      Permalink

      <%# Container.ItemIndex % 2==0 ? "white" : "whitesmoke" % How can I write this?: <%# Container.ItemIndex % 2==0 ? <%#Eval("companyname") : a href link with

    • February 6, 2012 at 3:48 pm
      Permalink

      This was a life safer had to make a few changes for ASP 2.0 because I am writing in VB. If anyone else is writing in VB there you go.

      Public Function IIf(ByVal Expression As Boolean, ByVal TruePart As Object, ByVal FalsePart As Object) As Object
      If Expression = True Then
      Return TruePart
      End If
      Return FalsePart
      End Function

    Leave a Reply

    Your email address will not be published. Required fields are marked *