Click here to Skip to main content
11,496,146 members (599 online)
Click here to Skip to main content

Tagged as

ASP.NET Conditions in Markup Using Bound Data

, 18 Oct 2012 CPOL 27.8K 6
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.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

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

<%# If Eval("YadaYada") Then %>

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

<% 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:

<%@ 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:

<%@ 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)

Share

About the Author

AspDotNetDev
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

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 19 Oct 2012
Article Copyright 2012 by AspDotNetDev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid