Listview / DetailsView: hide empty field

I guess this is a fairly common problem, but I haven't found an elegant solution yet.

I have multiple instances where I have a ListView or DetailsView control associated with a SQL Server SProc. The problem is that there are many cases where, when the column is Null, I want to display something different in the UI. A typical example would be if I have a URL column that appears as a LinkButton (in ListViews) or as a HyperLinkField (in DetailsViews) - when a Null URL is returned, I handle links without the src attribute. Ideally, I want to show nothing in this field in a scenario like this.

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

Thanks in advance.

Update: I haven't had a chance to try this, but all good advice. I think I love that Ricks answers all the best, but thanks again to others ...

+2


source to share


3 answers


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;

}

      

+2


source


Within template binding also to visibility

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

      



Warning: not tested, may also be

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

      

+1


source


I suppose you could either create a method in your code behind that takes a value as a parameter and returns a reference if it's not null. Or you can click on the data event in the Listview, check the value and hide the control if it is null. Not a very nifty solution, but I guess that's up to you. :)

+1


source







All Articles