Click here to Skip to main content
15,789,840 members
Articles / Web Development / ASP.NET
Tip/Trick

ASP.NET Conditions in Markup Using Bound Data

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 Oct 2012CPOL 38.5K   7   1
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)


Written By
Web Developer
United States United States

  • Managing Your JavaScript Library in ASP.NET (if you work with ASP.net and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

Comments and Discussions

 
QuestionWorks! Pin
Member 1153559926-Aug-15 7:16
Member 1153559926-Aug-15 7:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.