在ListView中实现排序

此处介绍的情境是:

(1)使用table布局ListView。

(2)ListView的数据源是List<T>。

(3)排序字段2个(帖子的回复次数和浏览次数),都是int类型。

 

基本思路:

ListView触发数据源排序,使用数据源(即List<T>)的Sort()方法,重新绑定数据源到ListView。

 

实现步骤:

(1)可查知,List<T>的Sort()方法带有一个ICompare<T>泛型接口类型的形参。所以,首先构造继承该泛型接口的类型:

    /// <summary>
    /// 回复升序比较类
    /// </summary>
    public class PostReplyCountAscCompare : IComparer<PostInfo>
    {
        #region IComparer<PostInfo> 成员

        public int Compare(PostInfo x, PostInfo y)
        {
            return x.ReplyCount.CompareTo(y.ReplyCount);
        }

        #endregion
    }
    /// <summary>
    /// 回复降序比较类
    /// </summary>
    public class PostReplyCountDescCompare : IComparer<PostInfo>
    {
        #region IComparer<PostInfo> 成员

        public int Compare(PostInfo x, PostInfo y)
        {
            return y.ReplyCount.CompareTo(x.ReplyCount);
        }

        #endregion
    }


    /// <summary>
    /// 浏览升序比较类
    /// </summary>
    public class PostViewCountAscCompare : IComparer<PostInfo>
    {
        #region IComparer<PostInfo> 成员

        public int Compare(PostInfo x, PostInfo y)
        {
            return x.ViewCount.CompareTo(y.ViewCount);
        }

        #endregion
    }
    /// <summary>
    /// 浏览降序比较类
    /// </summary>
    public class PostViewCountDescCompare : IComparer<PostInfo>
    {
        #region IComparer<PostInfo> 成员

        public int Compare(PostInfo x, PostInfo y)
        {
            return y.ViewCount.CompareTo(x.ViewCount);
        }

        #endregion
    }

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


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

    

    public class SortHelper
    {
        /// <summary>
        /// 对集合进行排序——泛型方法
        /// </summary>
        /// <typeparam name="T1">集合中的对象类型</typeparam>
        /// <typeparam name="T2">排序类型</typeparam>
        /// <param name="collection">要排序的集合</param>
        /// <param name="comparer">排序器</param>
        public static void Sort<T1,T2>(List<T1> collection,T2 comparer) where T2:IComparer<T1>
        {
            collection.Sort(comparer);
        }
    }


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

<LayoutTemplate>
                    <table class="PostList">
                        <tr class="PostListHeader">
                            <td colspan="2">
                                标题
                            </td>
                            <td>
                                发布日期
                            </td>
                            <td>
                                <asp:LinkButton runat="server" ID="lbtnReply" Text="回复" CommandName="Sort" CommandArgument="ReplyCount"
                                    CssClass="sortLink"></asp:LinkButton>
                            </td>
                            <td>
                                <asp:LinkButton runat="server" ID="lbtnView" Text="浏览" CommandName="Sort" CommandArgument="ViewCount"
                                    CssClass="sortLink"></asp:LinkButton>
                            </td>
                            <td>
                                最后发表
                            </td>
                            <td>
                                删除
                            </td>
                        </tr>
                        <tr runat="server" id="itemPlaceholder">
                        </tr>
                    </table>
                    <div class="pager">
                        <asp:DataPager ID="pagerBottom" runat="server" PageSize="5">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonCssClass="command" FirstPageText="<<" PreviousPageText="<"
                                    RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowLastPageButton="false"
                                    ShowNextPageButton="false" ShowPreviousPageButton="true" />
                                <asp:NumericPagerField ButtonCount="7" CurrentPageLabelCssClass="current" NextPreviousButtonCssClass="command"
                                    NumericButtonCssClass="command" />
                                <asp:NextPreviousPagerField ButtonCssClass="command" LastPageText=">>" NextPageText=">"
                                    RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowLastPageButton="true"
                                    ShowNextPageButton="true" ShowPreviousPageButton="false" />
                            </Fields>
                        </asp:DataPager>
                    </div>
                </LayoutTemplate>

注意:上面LayoutTemplate中的两个LinkButton,用来作为用户排序接口。其CommandName属性为Sort(固定),CommandArgument分别为ReplyCount和ViewCount。


(4)ListView公开了两个与排序相关的事件:Sorting和Sorted。

        /// <summary>
        /// listview排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lvPosts_Sorting(object sender, ListViewSortEventArgs e)
        {
            //判断是否指定了排序字段
            if (string.IsNullOrEmpty(e.SortExpression))
            {
                return;
            }
            //数据源
            if (ViewState["posts"] != null)
            {
                posts = ViewState["posts"] as List<PostInfo>;
            }
            else
            {
                posts = new PostInfoBLL().GetAllPosts(begin, end);
                ViewState["posts"] = posts;
            }
            //升序还是降序
            if (ViewState["SortDirection"] != null)
            {
                  e.SortDirection=(SortDirection)ViewState["SortDirection"];
            }

            //按哪个字段排序
            if (e.SortExpression == "ReplyCount")
            {
                if (e.SortDirection == SortDirection.Ascending)
                {
                    //泛型方法调用
                    SortHelper.Sort<PostInfo, PostReplyCountAscCompare>(posts, new PostReplyCountAscCompare());
                    ViewState["SortDirection"] = SortDirection.Descending;
                }
                else
                {
                    SortHelper.Sort<PostInfo, PostReplyCountDescCompare>(posts, new PostReplyCountDescCompare());
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
            }
            else if (e.SortExpression == "ViewCount")
            {
                if (e.SortDirection == SortDirection.Ascending)
                {
                    SortHelper.Sort<PostInfo,PostViewCountAscCompare>(posts, new PostViewCountAscCompare());
                    ViewState["SortDirection"] = SortDirection.Descending;
                }
                else
                {
                    SortHelper.Sort<PostInfo,PostViewCountDescCompare>(posts, new PostViewCountDescCompare());
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
            }
            BindPosts(true);
        }


注意:上述方法中的数据源的获取和BindPosts()方法,读者可自行杜撰。

(5)运行界面如下图:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值