Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

ASP.NET: Using conditionals in 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();

}


aspnet-data-binding-expression-conditional 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. :)

Comments

outcoldman said:

I think you have a little misprint in your post. You wrote

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

But you wanted to write this:

<%# Iif(Container.ItemIndex % 2==0, "white", "whitesmoke") %>

# April 5, 2010 12:56 AM

dinoboy said:

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?

It works fine for me.

# April 5, 2010 2:38 AM

Mohamed Meligy said:

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 5, 2010 3:53 AM

DigiMortal said:

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

# April 9, 2010 10:40 PM

MarcJ said:

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

# April 14, 2010 1:18 PM

Amy said:

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

How can I write this?:

<%# Container.ItemIndex % 2==0 ? <%#Eval("companyname") : a href link with <%#Eval("Companyname") %>

# October 12, 2011 10:57 AM

Caleb said:

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

# February 6, 2012 10:48 AM