By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 2,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

Conditionally write a Template Item is data in field exists?

P: n/a
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>
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:OL**************@TK2MSFTNGP10.phx.gbl...
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>

Nov 18 '05 #2

P: n/a
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.

--
Scott
http://www.OdeToCode.com

On Fri, 30 Apr 2004 11:47:42 -0400, "D. Shane Fowlkes"
<sh**********@h-o-t-m-a-i-l.com> wrote:
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>


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.