Repeater: condition of a field being blank or not? to Tag or not to Tag

H

HockeyFan

I have the following inside a repeater:

<ItemTemplate>
<b><%#Container.DataItem("TitleText")%></b><br />
<%#Container.DataItem("ParagraphText")%>

<br /><br />
</ItemTemplate>


if for some reason, the TitleText is blank or null I'd like to not have
the <br/> tag. In other words, I only want the <br/> tag if TitleText
has something in it.
Can I do this, and if so, how best to accomplish?
 
K

Karl Seguin

There are a number of ways, the cleanest and one that'll scale the best is
to hook into the ItemDataBound event of your repeater and control the output
programatically.

For example, you could do:

<asp:literal id="Extra" runat="server"><br /><br /></asp:Literal>

and in the ItemDataBound do:

if (records are null)
{
((Literal)e.Item.FindControl("Extra")).Visible = false;
}

You can read up more at:
http://openmymind.net/index.aspx?documentId=8#4.2

Karl
 
H

HockeyFan

I ended up solving the problem as follows:

<ItemTemplate>
<b><%#Container.DataItem("TitleText")%></b><%#IsBlankOrNull(Container.DataItem("TitleText"),
"", "<br />")%>
<%#Container.DataItem("ParagraphText")%>

<br /><br />
</ItemTemplate>

and then the following in my base class:

Public Function IsBlankOrNull(ByVal blnValue As Object, ByVal
objValueIfTrue As String, ByVal objValueIfFalse As String) As String
If blnValue Is DBNull.Value Then
Return objValueIfFalse
ElseIf CType(blnValue, String) = Nothing Then
Return objValueIfFalse
Else
If CType(blnValue, String) = "" Then
Return objValueIfFalse
Else
Return objValueIfTrue
End If
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top