Click here to Skip to main content
12,077,753 members (48,892 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

29.4K views
6 bookmarked
Posted

ASP.NET Conditions in Markup Using Bound Data

, 18 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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):

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

You may also be interested in...

Comments and Discussions

 
QuestionWorks! Pin
Member 1153559926-Aug-15 7:16
memberMember 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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160212.1 | Last Updated 19 Oct 2012
Article Copyright 2012 by AspDotNetDev
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid