开发者

listview problem

How to开发者_开发知识库 check if an a field is not empty, and show a link?

i tried something like this but i get error.

<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
    <h2><%#Eval("NC_Title") %></h2>
    <%#Eval("NC_StartDate") %>
    <p><%#Eval("NC_Abstract") %></p>
    <%if(Eval("NC_Description").ToString().Length > 0)
      {
          %><a href="">Read more...</a><%
      }%>

</ItemTemplate>
</asp:ListView>


Alternatively you can put the entire link into databinding:

<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
    <h2><%#Eval("NC_Title") %></h2>
    <%#Eval("NC_StartDate") %>
    <p><%#Eval("NC_Abstract") %></p>
    <%# !string.IsNullOrEmpty(Eval("NC_Description") as string) ? "<a href=\"\">Read more...</a>" : string.Empty %>
</ItemTemplate>
</asp:ListView>


You can make the "read more" link a control (ie. add runat="server"), and bind it's visibility:

<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
    <h2><%#Eval("NC_Title") %></h2>
    <%#Eval("NC_StartDate") %>
    <p><%#Eval("NC_Abstract") %></p>
    <a href="" runat="server" Visible='<%# !string.IsNullOrEmpty(Eval("NC_Description") as string) %>'>Read more...</a>
</ItemTemplate>
</asp:ListView>


Probably your NC_Description is null. In this case when you try to invoke ToString method from a null object it gives you NullReferenceException. Try to change it like this:

<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
    <h2><%#Eval("NC_Title") %></h2>
    <%#Eval("NC_StartDate") %>
    <p><%#Eval("NC_Abstract") %></p>
    <%if(!string.IsNullOrEmpty(Eval("NC_Description") as string))
      {
          %><a href="">Read more...</a><%
      }%>

</ItemTemplate>
</asp:ListView>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜