Click here to Skip to main content
Click here to Skip to main content

ASP.NET Conditions in Markup Using Bound Data

By , 18 Oct 2012
 

C# Version (See Alternatives for VB.NET 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")) { %>

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

<% if (Eval("YadaYada")) { %>

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="C#" AutoEventWireup="true" %>
 
<script runat="server">
 
  // Sample class we can use for binding.
  public class Animal
  {
    public Boolean CanFly { get; set; }
    public String Description { get; set; }
  }
 
  // Setup binding in page load.
  protected void Page_Load(Object sender,System.EventArgs e)
  {
    rpItems.DataSource = new List<Animal> {
        new Animal {CanFly = false, Description = "Duck"},
        new Animal {CanFly = true, Description = "Duck"},
        new Animal {CanFly = false, Description = "Duck"},
        new Animal {CanFly = true, Description = "Goose!"}};
    rpItems.DataBind();
  }
 
</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="<%# ((Animal)Container.DataItem).CanFly %>">
              <b>I can fly!</b>
            </asp:PlaceHolder>
            <asp:PlaceHolder runat="server" 
            Visible="<%# !((Animal)Container.DataItem).CanFly %>">
              I can't fly.
            </asp:PlaceHolder>
            <%# HttpUtility.HtmlEncode(((Animal)Container.DataItem).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="C#" AutoEventWireup="true" %>
 
<script runat="server">
 
  // Sample class we can use for binding.
  public class Animal
  {
    public Boolean CanFly { get; set; }
    public String Description { get; set; }
  }
 
  // This property is used in the bound control.
  protected Boolean CanFlyTemp { get; set; }
 

  // Setup binding in page load.
  protected void Page_Load(Object sender,System.EventArgs e)
  {
    rpItems.DataSource = new List<Animal> {
        new Animal {CanFly = false, Description = "Duck"},
        new Animal {CanFly = true, Description = "Duck"},
        new Animal {CanFly = false, Description = "Duck"},
        new Animal {CanFly = true, Description = "Goose!"}};
    rpItems.DataBind();
  }
 
</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="<%# ((Animal)Container.DataItem).CanFly %>">
            <% CanFlyTemp = true; %>
          </asp:PlaceHolder>
 
          <!-- Here we will use different markup 
          depending on the variable value set above. -->
          <li>
            <% if (CanFlyTemp) { %>
              <b>I can fly!</b>
            <% } else {%>
              I can't fly.
            <% }%>
            <%# HttpUtility.HtmlEncode(((Animal)Container.DataItem).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)

About the Author

AspDotNetDev
Web Developer
United States United States
Member
  • 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):

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralIs it the same tip posted twice?! Original tip and as altern...mvpPrerak Patel12 Jan '12 - 21:47 
GeneralRe: Yep, except one is in C# and one is in VB.net.protectorAspDotNetDev13 Jan '12 - 4:07 
GeneralRe: Yep, except one is in C# and one is in VB.net.subeditorIndivara18 Oct '12 - 20:49 
GeneralRe: Yep, except one is in C# and one is in VB.net.protectorAspDotNetDev19 Oct '12 - 3:48 
GeneralRe: Yep, except one is in C# and one is in VB.net.subeditorIndivara19 Oct '12 - 4:35 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 19 Oct 2012
Article Copyright 2012 by AspDotNetDev
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid