Listview/DetailsView: Hide a null field

不打扰是莪最后的温柔 提交于 2019-12-06 12:39:46

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;

}

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 %> />

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!