Connecting Tech Pros Worldwide Forums | Help | Site Map

Conditionally write a Template Item is data in field exists?

D. Shane Fowlkes
Guest
 
Posts: n/a
#1: Nov 18 '05
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>



Rick Spiewak
Guest
 
Posts: n/a
#2: Nov 18 '05

re: Conditionally write a Template Item is data in field exists?


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" <shanefowlkes@h-o-t-m-a-i-l.com> wrote in message
news:OLTCgrsLEHA.3324@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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[/color]
contains[color=blue]
> 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[/color]
do[color=blue]
> is check to see if "Address2" (field) is empty and/or null. If so then[/color]
skip[color=blue]
> 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[/color]
to[color=blue]
> 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") %>[/color]
<%#[color=blue]
> 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>
>
>[/color]


Scott Allen
Guest
 
Posts: n/a
#3: Nov 18 '05

re: Conditionally write a Template Item is data in field exists?


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"
<shanefowlkes@h-o-t-m-a-i-l.com> wrote:
[color=blue]
>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>
>[/color]

Closed Thread