X

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();
}

We 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. :)

Liked this post? Empower your friends by sharing it!
Categories: ASP.NET

View Comments (6)

  • 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?

  • 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

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

  • 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

Related Post