Conditional Logic in ASP.net page

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>

Leave a Comment