Click here to Skip to main content
15,886,059 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

ASP.NET Conditions in Markup Using Bound Data

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Oct 2012CPOL 20.9K   6   5
C# version

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

ASP.NET
<%# if (Eval("YadaYada")) { %>

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

ASP.NET
<% 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:

ASP.NET
<%@ 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:

ASP.NET
<%@ 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)


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

 
GeneralIs it the same tip posted twice?! Original tip and as altern... Pin
Prerak Patel12-Jan-12 21:47
professionalPrerak Patel12-Jan-12 21:47 
GeneralRe: Yep, except one is in C# and one is in VB.net. Pin
AspDotNetDev13-Jan-12 4:07
protectorAspDotNetDev13-Jan-12 4:07 
GeneralRe: Yep, except one is in C# and one is in VB.net. Pin
Indivara18-Oct-12 20:49
professionalIndivara18-Oct-12 20:49 
GeneralRe: Yep, except one is in C# and one is in VB.net. Pin
AspDotNetDev19-Oct-12 3:48
protectorAspDotNetDev19-Oct-12 3:48 
GeneralRe: Yep, except one is in C# and one is in VB.net. Pin
Indivara19-Oct-12 4:35
professionalIndivara19-Oct-12 4:35 

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.