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">
public class Animal
{
public Boolean CanFly { get; set; }
public String Description { get; set; }
}
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">
<asp:Repeater runat="server" ID="rpItems">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<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">
public class Animal
{
public Boolean CanFly { get; set; }
public String Description { get; set; }
}
protected Boolean CanFlyTemp { get; set; }
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">
<asp:Repeater runat="server" ID="rpItems">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<% CanFlyTemp = false; %>
<asp:PlaceHolder runat="server"
Visible="<%# ((Animal)Container.DataItem).CanFly %>">
<% CanFlyTemp = true; %>
</asp:PlaceHolder>
<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>