I have some code that prints out databse values into a repeater control on an asp.net page. However, some of the values returned are null/blank - and this makes the result
I suggest wrapping each key/value pair into custom control with 2 properties. This control will display itself only if value is not empty:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ShowPair.ascx.cs" Inherits="MyWA.ShowPair" %>
<% if (!string.IsNullOrEmpty(Value))
{ %>
<%=Key %> : <%=Value %>
<% } %>
And then put controls into repeater template:
<asp:Repeater runat='server' ID="repeater1">
<ItemTemplate>
<cst:ShowPair Key="Company Name:" Value="<%#((Company)Container.DataItem).CompanyName %>" runat="server"/>
<cst:ShowPair Key="Contact Name:" Value="<%#((Company)Container.DataItem).ContactName %>" runat="server" />
<cst:ShowPair Key="Address 1:" Value="<%#((Company)Container.DataItem).Address1 %>" runat="server" />
</ItemTemplate>
</asp:Repeater>
You can use IsDBNull(obj)
If IsDbNull(<%#Eval("Address2")%>) then
etc
End If
It's going to be a pretty subjective one this as it completely depends on where and how you like to handle null / blank values, and indeed which one of those two you are dealing with.
For example, some like to handle nulls at the database level, some like to code default values in the business logic layer and others like to handle default / blank values at the UI - not to mention the plethora of options in between.
Either way my personal choice would be to make sure you display that no data was available for that field at the UI level to avoid confusion. At worst something along the lines of:
<strong><% If (Eval("Address2").Length > 0) Then %><%#Eval("Address2")%><% Else %>No data available for Address 2<% End If %></strong><br />
That way at least the user knows that no data is available, rather than not knowing if there has been some system / administrative error.
Hope that helps :)
There are may ways to do that, I'm usually using repeater's event OnItemDataBound event that occurs when repeater's item is bound to a data item.
To explain OnItemDataBound event let's assume that we have repeater with one field that is always displayed (Name) and optional field that is displayed if is not null (Optional). Further more we want to display some predefined value if optional field is null or empty.
To do this we need first to set repeater's OnItemDataBound event to point to a method, And also to build repeater's item template. We could use any server control within repeater's item template that we can reference later in OnItemDataBound method.
<asp:Repeater ID="repeaterResults" runat="server" OnItemDataBound="repeaterResult_ItemDataDataBound">
<ItemTemplate>
<strong><%#Eval("Name") %></strong>
<asp:Literal runat="server" ID="ltlOption" />
<br />
</ItemTemplate></asp:Repeater>
Further let's suppose that we will bind a collection of simple objects that are having two properties :Name and Option like follows:
public class SimpleEntity
{
public string Name {get;set;}
public string Option {get;set;}
}
Next we will implement repeaterResult_ItemDataDataBound method as follows:
protected void repeaterResult_ItemDataDataBound(object sender, RepeaterItemEventArgs e)
{
SimpleEntity ent = e.Item.DataItem as SimpleEntity;
Literal ltlOption = e.Item.FindControl("ltlOption") as Literal;
if (ent != null && ltlOption != null)
{
if (!string.IsNullOrEmpty(ent.Option))
{
ltlOption.Text = ent.Option;
}
else
{
ltlOption.Text = "Not entered!";
}
}
}
As method above is implemented, we will display optional field if exists, while if optional field is null or empty string we will display predefined string "Not entered!".
I realise that this is a very old question, but I'd like to add that perhaps the best way of handling this issue is more at the database level, and yes - I know that the OP hasn't specified any type of data source.
I'm simply going to assume (yes - ass of you and me) that the current language being used is at least Transact SQL.
To this end, I tend to use the data source to produce compound fields. In the case of an address ISNULL will happily test to see which fields are in use, and return a default value if a NULL field is encountered. On top of this, a separator character(s) can be included to allow for line breaks in the target output medium. One option is to use comma + one space as a delimiter ', '
.
SELECT
ISNULL(src.address1 + ', ', '') +
ISNULL(src.address2 + ', ', '') +
ISNULL(src.address3 + ', ', '') +
ISNULL(src.address4 + ', ', '') +
ISNULL(src.postalcode, '') AS CompoundAddress
...
This works by using NULL
against itself - adding to NULL
returns a NULL
, therefore the value returned will either contain our comma + space or it will return an empty string.
Something similar can be done to 'trick' Microsoft Access into producing your address field...
SELECT
(src.address1 + ', ') &
(src.address2 + ', ') &
(src.address3 + ', ') &
(src.address4 + ', ') &
(src.postalcode) As CompoundAddress
...
In this case the ampersand will convert the NULL
to an empty string, but the same still applies to the the addition of the string to the potentially NULL
field.
So now, we can output our address properly in the HTML...
<div id="results">
<asp:Repeater ID="repeaterResults" runat="server">
<ItemTemplate>
Company: <strong><%#Eval("CompanyName") %></strong><br />
Contact Name: <strong><%#Eval("ContactName") %></strong><br />
Address: <strong><%#Eval("CompoundAddress").ToString().Replace(", ", "<br />") %></strong><br />