ASP.NET Conditions in Markup Using Bound Data





0/5 (0 vote)
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):
<%# 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>