none
在ListView绑定ObjectDataSource后,怎样根据字段是否为空设置字段是否显示 RRS feed

  • 问题

  • 如题,绑定后,有些字段是空值,
    怎样处理为:某一字段为空就不显示,不为空就显示
    例如:一页显示10条记录,Content字段在第一条记录不为空,就显示出来,第二条记录中为空,就不显示。
    由于代码分离,asp.net中很多数据空间都无法根据数据内容进行判断了,真的挺麻烦啊。

    求教高手了

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" DataSourceID="ObjectDataSource1" >
      <ItemTemplate>
        <li style="">ID:
          <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
          <br />
          Title:
          <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
          <br />
          Content:
          <asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>' />
          <br />
          <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
          <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
        </li>
      </ItemTemplate>
    </asp:ListView>
    
    2011年3月23日 9:10

答案

  • 给你一个完整的例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    ///BlogUser 的摘要说明
    /// </summary>
    public class BlogUser
    {
     public String UserName { set; get; }
     public String Data { set; get; }
     public BlogUser() { }
     public BlogUser(String name, String data)
     {
      this.UserName = name;
      this.Data = data;
     }
    
     public List<BlogUser> GetBlogUsers()
     {
      List<BlogUser> blogUsers = new List<BlogUser>();
      blogUsers.Add(new BlogUser("A", "aaa"));
      blogUsers.Add(new BlogUser("B", "bbb"));
      blogUsers.Add(new BlogUser("C", ""));
      blogUsers.Add(new BlogUser("D", "bbb"));
      blogUsers.Add(new BlogUser("E", ""));
      blogUsers.Add(new BlogUser("F", "bbb"));
      return blogUsers;
     }
    }
    
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
     protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
     {
      if (e.Item.ItemType == ListViewItemType.DataItem)
      {
       BlogUser u = e.Item.DataItem as BlogUser;
       if (u.Data.Equals(""))
       {
        e.Item.Visible = false;
       }
      }
     }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
     <title></title>
    </head>
    <body>
     <form id="form1" runat="server">
     <asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1" 
      onitemdatabound="ListView1_ItemDataBound">
      <ItemTemplate>
       <li>UserName:<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("UserName") %>' />
        Data:<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Data") %>' />
      </ItemTemplate>
     </asp:ListView>
     <asp:ObjectDataSource ID="ObjectDataSource1" SelectMethod="GetBlogUsers" TypeName="BlogUser"
      runat="server"></asp:ObjectDataSource>
     </form>
    </body>
    </html>
    
    


    【孟子E章】
    2011年3月24日 1:14
    版主

全部回复

  • 在绑定事件里进行判断
    【孟子E章】
    2011年3月23日 9:12
    版主
  • 版主,就是不会写判断的代码:(,不知道怎么搞
    2011年3月23日 9:21
  • 给你一个完整的例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    ///BlogUser 的摘要说明
    /// </summary>
    public class BlogUser
    {
     public String UserName { set; get; }
     public String Data { set; get; }
     public BlogUser() { }
     public BlogUser(String name, String data)
     {
      this.UserName = name;
      this.Data = data;
     }
    
     public List<BlogUser> GetBlogUsers()
     {
      List<BlogUser> blogUsers = new List<BlogUser>();
      blogUsers.Add(new BlogUser("A", "aaa"));
      blogUsers.Add(new BlogUser("B", "bbb"));
      blogUsers.Add(new BlogUser("C", ""));
      blogUsers.Add(new BlogUser("D", "bbb"));
      blogUsers.Add(new BlogUser("E", ""));
      blogUsers.Add(new BlogUser("F", "bbb"));
      return blogUsers;
     }
    }
    
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
     protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
     {
      if (e.Item.ItemType == ListViewItemType.DataItem)
      {
       BlogUser u = e.Item.DataItem as BlogUser;
       if (u.Data.Equals(""))
       {
        e.Item.Visible = false;
       }
      }
     }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
     <title></title>
    </head>
    <body>
     <form id="form1" runat="server">
     <asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1" 
      onitemdatabound="ListView1_ItemDataBound">
      <ItemTemplate>
       <li>UserName:<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("UserName") %>' />
        Data:<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Data") %>' />
      </ItemTemplate>
     </asp:ListView>
     <asp:ObjectDataSource ID="ObjectDataSource1" SelectMethod="GetBlogUsers" TypeName="BlogUser"
      runat="server"></asp:ObjectDataSource>
     </form>
    </body>
    </html>
    
    


    【孟子E章】
    2011年3月24日 1:14
    版主