2025年在ListView中实现排序

在ListView中实现排序此处介绍的情境是 1 使用 table 布局 ListView 2 ListView 的数据源是 List 3 排序字段 2 个 帖子的回复次数和浏览次数 都是 int 类型 基本思路 ListView 触发数据源排序 使用数据源 即 List 的 Sort 方法 又一次绑定数据源到 ListView 实现步骤 1 可查知 List 的 Sort 方法带有一个 ICompare 泛型接口类型的形參

此处介绍的情境是:

(1)使用table布局ListView。

(2)ListView的数据源是List

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

基本思路:

ListView触发数据源排序,使用数据源(即List)的Sort()方法,又一次绑定数据源到ListView。

实现步骤:

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

    /// 
/// 回复升序比較类
///

public class PostReplyCountAscCompare : IComparer
{
#region IComparer 成员

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

#endregion
}
///
/// 回复降序比較类
///

public class PostReplyCountDescCompare : IComparer
{
#region IComparer 成员

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

#endregion
}


///
/// 浏览升序比較类
///

public class PostViewCountAscCompare : IComparer
{
#region IComparer 成员

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

#endregion
}
///
/// 浏览降序比較类
///

public class PostViewCountDescCompare : IComparer
{
#region IComparer 成员

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
{
///
/// 对集合进行排序——泛型方法
///

/// 集合中的对象类型
/// 排序类型
/// 要排序的集合
/// 排序器
public static void Sort(List collection,T2 comparer) where T2:IComparer
{
collection.Sort(comparer);
}
}

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














标题

公布日期

CssClass="sortLink">

CssClass="sortLink">

最后发表

删除




RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowLastPageButton="false"
ShowNextPageButton="false" ShowPreviousPageButton="true" />
NumericButtonCssClass="command" />
>" NextPageText=">"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowLastPageButton="true"
ShowNextPageButton="true" ShowPreviousPageButton="false" />



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

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

        /// 
/// listview排序
///

///
///
protected void lvPosts_Sorting(object sender, ListViewSortEventArgs e)
{
//推断是否指定了排序字段
if (string.IsNullOrEmpty(e.SortExpression))
{
return;
}
//数据源
if (ViewState["posts"] != null)
{
posts = ViewState["posts"] as List;
}
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(posts, new PostReplyCountAscCompare());
ViewState["SortDirection"] = SortDirection.Descending;
}
else
{
SortHelper.Sort(posts, new PostReplyCountDescCompare());
ViewState["SortDirection"] = SortDirection.Ascending;
}
}
else if (e.SortExpression == "ViewCount")
{
if (e.SortDirection == SortDirection.Ascending)
{
SortHelper.Sort(posts, new PostViewCountAscCompare());
ViewState["SortDirection"] = SortDirection.Descending;
}
else
{
SortHelper.Sort(posts, new PostViewCountDescCompare());
ViewState["SortDirection"] = SortDirection.Ascending;
}
}
BindPosts(true);
}

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

(5)执行界面例如以下图:


编程小号
上一篇 2025-01-19 15:17
下一篇 2025-01-19 15:06

相关推荐

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