Conditionally write a Template Item is data in field exists?

D

D. Shane Fowlkes

I'm using ASP.NET (VB) and I'm pretty new to .NET and using Templates.

I have a SqlDataReader which is bound to a Repeater. The Repeater contains
a table and for each record, it'll write a table. Easy stuff so far. My
table contains typical Address and Phone numbers data. What I'd like to do
is check to see if "Address2" (field) is empty and/or null. If so then skip
the line all together, but if it does contain something, write:

<%# Container.DataItem("Address2") %><br />

Make sense? I'm trying to avoid having an empty line between Address2 and
City, State Zip. I've experimented with a "helper function" by sending
Address2 to a Function and have it send back nothing or literally the HTML
to complete this line. But again, if it's null, it errors. Hmmm. What to
do??

I would appreciate any pointers or advice.


<asp:Repeater ID="rptProviders" Runat="Server">
<ItemTemplate>

<table width="95%" border="0" align="center" cellpadding="5"
cellspacing="0" class="grid">
<tr>
<td class="colheader">(<% =intGlobalCounter %>) <%#
Container.DataItem("OrgName") %></td>
</tr>

<tr>
<td>
<p align="center"><img src="../assets/providers/<%#
Container.DataItem("Photo") %>" class="imgborder" border="0" /></p>
<p><%# Container.DataItem("ProfileDescription") %></p>
<p>Serving: Area 1, Area 2, Area 3</p>
<p><%# Container.DataItem("Address") %><br />
<%# Container.DataItem("Address2") %><br />
<%# Container.DataItem("City") %>, <%# Container.DataItem("State") %> <%#
Container.DataItem("Zip") %><br />
<%# Container.DataItem("Phone") %><br />
<%# Container.DataItem("Fax") %><br />
<a href="mailto:<%# Container.DataItem("ContactEmail") %>"><%#
Container.DataItem("ContactEmail") %></a><br />
<a href="<%# Container.DataItem("WebSite") %>" target="_blank"><%#
Container.DataItem("WebSite") %></a></p></td>
</tr>
</table>
<% intGlobalCounter = intGlobalCounter + 1 %>
</ItemTemplate>
</asp:Repeater>
 
R

Rick Spiewak

You can use something like this:

Public Shared Function no_null_string(ByVal value As Object) As String
If value Is System.DBNull.Value Then
Return String.Empty
Else
Return value.ToString
End If
End Function
 
S

Scott Allen

I'm sure a helper function could pull this off. What sort of error
were you getting and what did the code look like? You might just need
a little bit of logic to avoid a null reference.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top