Chinaunix首页 | 论坛 | 博客
  • 博客访问: 828173
  • 博文数量: 190
  • 博客积分: 2991
  • 博客等级: 少校
  • 技术积分: 2400
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-24 18:11
文章分类

全部博文(190)

文章存档

2015年(3)

2014年(1)

2013年(65)

2012年(121)

我的朋友

分类: 系统运维

2012-11-22 15:44:30

此处介绍的情境是:

(1)使用table布局ListView。
(2)ListView的数据源是List
(3)排序字段2个(帖子的回复次数和浏览次数),都是int类型。
基本思路:
ListView触发数据源排序,使用数据源(即List)的Sort()方法,重新绑定数据源到ListView。
实现步骤:
(1)可查知,List的Sort()方法带有一个ICompare泛型接口类型的形参。所以,首先构造继承该泛型接口的类型:
[csharp] view plaincopy
  1. ///   
  2. /// 回复升序比较类  
  3. ///   
  4. public class PostReplyCountAscCompare : IComparer  
  5. {  
  6.     #region IComparer 成员  
  7.   
  8.     public int Compare(PostInfo x, PostInfo y)  
  9.     {  
  10.         return x.ReplyCount.CompareTo(y.ReplyCount);  
  11.     }  
  12.     #endregion  
  13. }  
  14. ///   
  15. /// 回复降序比较类  
  16. ///   
  17. public class PostReplyCountDescCompare : IComparer  
  18. {  
  19.     #region IComparer 成员  
  20.     public int Compare(PostInfo x, PostInfo y)     {  
  21.         return y.ReplyCount.CompareTo(x.ReplyCount);  
  22.     }  
  23.     #endregion  
  24. }  
  25. ///   
  26. /// 浏览升序比较类  
  27. ///   
  28. public class PostViewCountAscCompare : IComparer  
  29. {  
  30.     #region IComparer 成员  
  31.     public int Compare(PostInfo x, PostInfo y)  
  32.     {  
  33.         return x.ViewCount.CompareTo(y.ViewCount);  
  34.     }  
  35.     #endregion  
  36. }  
  37. ///   
  38. /// 浏览降序比较类  
  39. ///   
  40. public class PostViewCountDescCompare : IComparer  
  41. {  
  42.     #region IComparer 成员  
  43.   
  44.    public int Compare(PostInfo x, PostInfo y)  
  45.     {  
  46.         return y.ViewCount.CompareTo(x.ViewCount);  
  47.     }  
  48.     #endregion  
  49. }  

注意:上述的PostInfo模型类,读者可以杜撰,但要有ViewCount和ReplyCount属性(int类型)。

(2)由于有4个排序规则,对应上述(1)中的4个类。所以构造一个排序辅助类:SortHelper,代码如下:

[csharp] view plaincopy
  1. public class SortHelper  
  2. {  
  3.     ///   
  4.     /// 对集合进行排序——泛型方法  
  5.     ///   
  6.     /// 集合中的对象类型  
  7.     /// 排序类型  
  8.     /// 要排序的集合  
  9.     /// 排序器  
  10.     public static void Sort(List collection,T2 comparer) where T2:IComparer  
  11.     {  
  12.         collection.Sort(comparer);  
  13.     }  
  14. }  

(3)设计ListView,构造排序字段

  1. <LayoutTemplate>  
  2.                     <table class="PostList">  
  3.                         <tr class="PostListHeader">  
  4.                             <td colspan="2">  
  5.                                 标题  
  6.                             td>  
  7.                             <td>  
  8.                                 发布日期  
  9.                             td>  
  10.                             <td>  
  11.                                 <asp:LinkButton runat="server" ID="lbtnReply" Text="回复" CommandName="Sort" CommandArgument="ReplyCount"  
  12.                                     CssClass="sortLink">asp:LinkButton>  
  13.                             td>  
  14.                             <td>  
  15.                                 <asp:LinkButton runat="server" ID="lbtnView" Text="浏览" CommandName="Sort" CommandArgument="ViewCount"  
  16.                                     CssClass="sortLink">asp:LinkButton>  
  17.                             td>  
  18.                             <td>  
  19.                                 最后发表  
  20.                             td>  
  21.                             <td>  
  22.                                 删除  
  23.                             td>  
  24.                         tr>  
  25.                         <tr runat="server" id="itemPlaceholder">  
  26.                         tr>  
  27.                     table>  
  28.                     <div class="pager">  
  29.                         <asp:DataPager ID="pagerBottom" runat="server" PageSize="5">  
  30.                             <Fields>  
  31.                                 <asp:NextPreviousPagerField ButtonCssClass="command" FirstPageText="<<" PreviousPageText="<"  
  32.                                     RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowLastPageButton="false"  
  33.                                     ShowNextPageButton="false" ShowPreviousPageButton="true" />  
  34.                                 <asp:NumericPagerField ButtonCount="7" CurrentPageLabelCssClass="current" NextPreviousButtonCssClass="command"  
  35.                                     NumericButtonCssClass="command" />  
  36.                                 <asp:NextPreviousPagerField ButtonCssClass="command" LastPageText=">>" NextPageText=">"  
  37.                                     RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowLastPageButton="true"  
  38.                                     ShowNextPageButton="true" ShowPreviousPageButton="false" />  
  39.                             Fields>  
  40.                         asp:DataPager>  
  41.                     div>  
  42.                 LayoutTemplate>  
注意:上面LayoutTemplate中的两个LinkButton,用来作为用户排序接口。其CommandName属性为Sort(固定),CommandArgument分别为ReplyCount和ViewCount。
(4)ListView公开了两个与排序相关的事件:Sorting和Sorted。
[csharp] view plaincopy
  1. ///   
  2. /// listview排序  
  3. ///  
  4. ///   
  5. ///   
  6. protected void lvPosts_Sorting(object sender, ListViewSortEventArgs e)  
  7. {  
  8.     //判断是否指定了排序字段  
  9.     if (string.IsNullOrEmpty(e.SortExpression))  
  10.     {  
  11.         return;  
  12.     }  
  13.     //数据源  
  14.     if (ViewState["posts"] != null)  
  15.     {  
  16.         posts = ViewState["posts"as List;  
  17.     }  
  18.     else  
  19.     {  
  20.         posts = new PostInfoBLL().GetAllPosts(begin, end);  
  21.         ViewState["posts"] = posts;  
  22.     }  
  23.     //升序还是降序  
  24.     if (ViewState["SortDirection"] != null)  
  25.     {  
  26.           e.SortDirection=(SortDirection)ViewState["SortDirection"];  
  27.     }  
  28.     //按哪个字段排序  
  29.     if (e.SortExpression == "ReplyCount")  
  30.     {  
  31.         if (e.SortDirection == SortDirection.Ascending)  
  32.         {  
  33.             //泛型方法调用  
  34.             SortHelper.Sort(posts, new PostReplyCountAscCompare());  
  35.             ViewState["SortDirection"] = SortDirection.Descending;  
  36.         }  
  37.         else  
  38.         {  
  39.             SortHelper.Sort(posts, new PostReplyCountDescCompare());  
  40.             ViewState["SortDirection"] = SortDirection.Ascending;  
  41.         }  
  42.     }  
  43.     else if (e.SortExpression == "ViewCount")  
  44.     {  
  45.         if (e.SortDirection == SortDirection.Ascending)  
  46.         {  
  47.             SortHelper.Sort(posts, new PostViewCountAscCompare());  
  48.             ViewState["SortDirection"] = SortDirection.Descending;  
  49.         }  
  50.         else  
  51.         {  
  52.             SortHelper.Sort(posts, new PostViewCountDescCompare());  
  53.             ViewState["SortDirection"] = SortDirection.Ascending;  
  54.         }  
  55.     }  
  56.     BindPosts(true);  
  57. }  
注意:上述方法中的数据源的获取和BindPosts()方法,读者可自行杜撰。
(5)运行界面如下图:

原文地址:

阅读(1003) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~