在ListView中实现排序

在ListView中实现排序此处介绍的情境是:(1)使用table布局ListView。(2)ListView的数据源是List。(3)排序字段2个(帖子的回复次数和浏览次数),都是int类型。基本思路: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)执行界面例如以下图:

<span role="heading" aria-level="2">在ListView中实现排序

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155506.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • v8,spidermonkey,chakra,spidermonkey四大主流JS引擎安装教程

    v8,spidermonkey,chakra,spidermonkey四大主流JS引擎安装教程四大主流JS引擎安装教程参考网址:https://github.com/sslab-gatech/DIE/tree/master/engines安装步骤:./download-engine.shch1.11.5./download-engine.shjsc2.23.3./build-ch.sh1.11.5./build-jsc.sh2.23.3脚本含义:./download-engine.sh:用于下载引擎源码,ch对应chakra引擎,1.11.5对应引擎版本。另:jsc

    2022年10月16日
  • *.asmx文件漏洞搜索「建议收藏」

    *.asmx文件漏洞搜索「建议收藏」AdminWebServicehttp://<AdminSite>/_vti_adm/Admin.asmxProvidesmethodsformanagingadeploymentofMicrosoftWindowsSharePointServices,suchasforcreatingordeletingsitec…

    2022年5月4日
  • C语言文件读写操作(详解)

    C语言文件读写操作(详解)数据流和缓冲区是什么?文件类型和文件存取方式都有啥?数据流就C程序而言,从程序移进,移出字节,这种字节流就叫做流。程序与数据的交互是以流的形式进行的。进行C语言文件的读写时,都会先进行“打开文件”操作,这个操作就是在打开数据流,而“关闭文件”操作就是关闭数据流。缓冲区在程序执行时,所提供的额外内存,可用来暂时存放准备执行的数据。它的设置是为了提高存取效率,因为内存的存取速度比磁盘驱动器快得多。当使用标准I/O函数(包含在头文件stdio.h中)时,系统会自动设置缓冲区,并通过数据流来读写文件。当

    2022年6月2日
  • 打开VS2010提示:产品密钥框

    打开VS2010提示:产品密钥框                                          &n

    2022年6月5日
  • PS2手柄遥控控制灯开关(arduino)

    PS2手柄遥控控制灯开关(arduino)PS2手柄遥控控制灯开关(arduino)使用的材料arduino板子:ps2遥控手柄;一个5V供电的LDE;连线首先按照遥控手柄底座上的英文,你会发现一共有六根线,分别是GND,VCC,DAT,CMD,CS,CL。GND连接arduino板子上的gnd,vcc连接5V或者3.3v,其他四根线可以按照代码中的引脚设置连接,也可以自己更改。代码部分#include<PS…

    2022年6月3日
  • 人体检测–热释电传感器开发

    人体检测–热释电传感器开发人体检测–热释电传感器开发人体热释电传感器顾名思义是探测是否有人体通行和通过,由于它的廉价性,使得它的应用范围非常广泛。楼道里的灯,天台的报警设施等,都是利用这个来进行报警和检测。本文章将分为两个板块来介绍传感器的开发和应用。一·热释电传感器的工作原理  某些晶体,例如钽酸锂、硫酸三甘肽等受热时,晶体两端会产生数量相等、符号相反的电荷。1842年布鲁斯特将这种由温度变化引起的电极化现象正式命名为…

    2022年9月1日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号