Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Logic in ASP.net page

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

like image 796
Vidar Avatar asked Dec 15 '08 12:12

Vidar


2 Answers

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>
like image 150
Ihar Voitka Avatar answered Sep 20 '22 01:09

Ihar Voitka


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

like image 41
Matt Woodward Avatar answered Sep 21 '22 01:09

Matt Woodward