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. 🙂


Leave a Reply

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