views:

3139

answers:

4

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 look ugly when there are blank spaces.

How do you do conditional logic in asp.net controls i.e. print out a value if one exists, else just go to next value.

I should also add - that I want the markup to be conditional also, as if there is no value I dont want a
tag either.

Here is a snippet of code below just to show the type of values I am getting back from my database. (It is common for Address 2 not to have a value at all).

<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("Address1")%></strong><br />                    
                           <strong><%#Eval("Address2")%></strong><br />..................

Many thanks

+3  A: 

Hey Simon,

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 :)

Matt Woodward
+1  A: 

You can use IsDBNull(obj)

If IsDbNull(<%#Eval("Address2")%>) then
     etc
End If
Jeff.Crossett
This is C#: though IsDbNull() is available via the microsoft.visualbasic namespace
Joel Coehoorn
so use == DBNull.Value
Greg B
+1  A: 

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!".

Aleksandar
+2  A: 

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>
Ihar Voitka