Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

ASP.NET Conditions in Markup Using Bound Data

5.00/5 (1 vote)
18 Oct 2012CPOL 39.4K  
You can't use an if statement in a bound code block, but you can still use conditions in your markup based on bound data.

VB.NET Version (See Alternatives for C# Version)

Have you ever tried to use an if statement in combination with data binding? You can't do this (can't use an if statement in this type of code block):

ASP.NET
<%# If Eval("YadaYada") Then %>

And you can't do this (can't data bind in this type of code block):

ASP.NET
<% If Eval("YadaYada") Then %>

So how do you combine these concepts? Basically, you can use a PlaceHolder with a Visible property set to a bound value (or the result of a condition based on a bound value). Anything (code blocks or markup) inside an invisible PlaceHolder will not be executed. Here is one way to go about that:

ASP.NET
<%@ Page Language="vb" AutoEventWireup="false" %>
 
<script runat="server">
 
  ' Sample class we can use for binding.
  Public Class Animal
    Public Property CanFly As Boolean
    Public Property Description As String
  End Class
 
  ' Setup binding in page load.
  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    rpItems.DataSource = New List(Of Animal) From {
        New Animal With {.CanFly = False, .Description = "Duck"},
        New Animal With {.CanFly = True, .Description = "Duck"},
        New Animal With {.CanFly = False, .Description = "Duck"},
        New Animal With {.CanFly = True, .Description = "Goose!"}}
    rpItems.DataBind()
  End Sub
 
</script>
 
<html>
<head>
    <title>Markup Conditions Using Bound Code Block</title>
</head>
<body>
    <form id="frmMain" runat="server">
 
      <!-- This is our data bound control. -->
      <asp:Repeater runat="server" ID="rpItems">
        <HeaderTemplate>
          <ul>
        </HeaderTemplate>
        <ItemTemplate>
 
          <!--  We use placeholder visibility to use 
          different markup depending on the bound value. -->
          <li>
            <asp:PlaceHolder runat="server" 
            Visible="<%# DirectCast(Container.DataItem, Animal).CanFly %>">
              <b>I can fly!</b>
            </asp:PlaceHolder>
            <asp:PlaceHolder runat="server" 
            Visible="<%# Not DirectCast
            (Container.DataItem, Animal).CanFly %>">
              I can't fly.
            </asp:PlaceHolder>
            <%# HttpUtility.HtmlEncode
            (DirectCast(Container.DataItem, Animal).Description) %>
          </li>
 
        </ItemTemplate>
        <FooterTemplate>
          </ul>
        </FooterTemplate>
      </asp:Repeater>
 
    </form>
</body>
</html>

Alternatively, you can use the placeholders to set the value of a property or variable, which you can then use in code blocks that aren't data bound:

ASP.NET
<%@ Page Language="vb" AutoEventWireup="false" %>
 
<script runat="server">
 
  ' Sample class we can use for binding.
  Public Class Animal
    Public Property CanFly As Boolean
    Public Property Description As String
  End Class
 
  ' This property is used in the bound control.
  Protected Property CanFlyTemp As Boolean
 
  ' Setup binding in page load.
  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    rpItems.DataSource = New List(Of Animal) From {
        New Animal With {.CanFly = False, .Description = "Duck"},
        New Animal With {.CanFly = True, .Description = "Duck"},
        New Animal With {.CanFly = False, .Description = "Duck"},
        New Animal With {.CanFly = True, .Description = "Goose!"}}
    rpItems.DataBind()
  End Sub
 
</script>
 
<html>
<head>
    <title>Markup Conditions Using Bound Code Block</title>
</head>
<body>
    <form id="frmMain" runat="server">
 
      <!-- This is our data bound control. -->
      <asp:Repeater runat="server" ID="rpItems">
        <HeaderTemplate>
          <ul>
        </HeaderTemplate>
        <ItemTemplate>
 
          <!-- We use placeholder visibility to 
          conditionally execute code based on a bound value. -->
          <% CanFlyTemp = False%>
          <asp:PlaceHolder runat="server" 
          Visible="<%# DirectCast(Container.DataItem, Animal).CanFly %>">
            <% CanFlyTemp = True%>
          </asp:PlaceHolder>
 
          <!-- Here we will use different markup depending on the variable value set above. -->
          <li>
            <% If CanFlyTemp Then%>
              <b>I can fly!</b>
            <% Else%>
              I can't fly.
            <% End If%>
            <%# HttpUtility.HtmlEncode(DirectCast(Container.DataItem, Animal).Description) %>
          </li>
 
        </ItemTemplate>
        <FooterTemplate>
          </ul>
        </FooterTemplate>
      </asp:Repeater>
 
    </form>
</body>
</html>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)