views:

517

answers:

3

I imagine this is quite a common problem, but as yet I haven't found an elegant solution.

I have a number of instances where I have either a ListView or a DetailsView control that is bound to a SQL Server SProc. The problem I have is that there are numerous instances where, when a column is Null, I want to display something different in the UI. A typical example would be where I have a URL column that is rendered as a LinkButton (in the ListViews) or as a HyperLinkField (in the DetailsViews) - when a Null URL is returned, I'm rendering links with no src attribute. Ideally, I's want to display nothing in this field in such a scenario.

In each of these cases, when a null value is returned, how can I skip/alter the rendering of that item?

Thanks in advance.

Update: I haven't had chance to actually try these out, but all helpful suggestions. I think I like Ricks answer the best, but thanks again to the others...

+1  A: 

Within a Template bind also to Visibility

<asp:HyperLink ... NavigateURL=<%# Eval("url") %> Visible=<%# Eval("url") != null %> />

Warning: not Tested, could also be

<asp:HyperLink ... NavigateURL=<%# Eval("url") %> Visible=<%# Eval("url") != DBNull.Value %> />
Arthur
+1  A: 

I suppose you could either create a method in your code behind that takes the value as a parameter and returns a link if it's not null. Or you could tap into the data bound event of the Listview, examine the value and hide the control if it's null. Neither a very elegant solutions, but I guess that's up to you to decide. :)

Jakob Gade
+2  A: 

Markup:

 <asp:HyperLink id="whatever" runat="server" 
  NavigateURL='<%# Eval("url") %>' Visible='<%# IsVisible(Eval("url")) %>' />

Code behind:

protected bool IsVisible(object obj)
{
     bool result = false;

     string url = (string)obj;
     if(!string.IsNullOrEmpty(url))
     {
          result = true;
     }

     return result;

}
rick schott