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">
Public Class Animal
Public Property CanFly As Boolean
Public Property Description As String
End Class
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">
<asp:Repeater runat="server" ID="rpItems">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<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">
Public Class Animal
Public Property CanFly As Boolean
Public Property Description As String
End Class
Protected Property CanFlyTemp As Boolean
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">
<asp:Repeater runat="server" ID="rpItems">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<% CanFlyTemp = False%>
<asp:PlaceHolder runat="server"
Visible="<%# DirectCast(Container.DataItem, Animal).CanFly %>">
<% CanFlyTemp = True%>
</asp:PlaceHolder>
<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>