datalist分页(三) :排序

datalist分页(三) :

 即然重写petshop中customLit.cs可以实现datalist的自定义分页(见二),但比起datagrid好像还差了个排序。

希望能实现泛型排序。不想直接跟数据库关联,当然也可以用设置dataview.sort来实现,

    幸好,阿三的神灯照亮了我前进的道路[参考1],当然阿三的PropertyComaparer<T>类也是借鉴了msdn.

贴一下简单实现泛型根据动态属性排序的代码:

PropertyComaparer.cs

 

using  System;
using  System.ComponentModel;
using  System.Collections.Generic;
using  System.Reflection;

///   <summary>
///  Summary description for PropertyComparer
///   </summary>
public   class  PropertyComparer < T >  : System.Collections.Generic.IComparer < T >
{

    
//  The following code contains code implemented by Rockford Lhotka:
    
//   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp

    
private  PropertyDescriptor _property;
    
private  ListSortDirection _direction;

    
public  PropertyComparer(PropertyDescriptor property, ListSortDirection direction)
    {
        _property 
=  property;
        _direction 
=  direction;
    }

    
#region  IComparer<T>

    
public   int  Compare(T xWord, T yWord)
    {
        
//  Get property values
         object  xValue  =  GetPropertyValue(xWord, _property.Name);
        
object  yValue  =  GetPropertyValue(yWord, _property.Name);

        
//  Determine sort order
         if  (_direction  ==  ListSortDirection.Ascending)
        {
            
return  CompareAscending(xValue, yValue);
        }
        
else
        {
            
return  CompareDescending(xValue, yValue);
        }
    }

    
public   bool  Equals(T xWord, T yWord)
    {
        
return  xWord.Equals(yWord);
    }

    
public   int  GetHashCode(T obj)
    {
        
return  obj.GetHashCode();
    }

    
#endregion

    
//  Compare two property values of any type
     private   int  CompareAscending( object  xValue,  object  yValue)
    {
        
int  result;

        
//  If values implement IComparer
         if  (xValue  is  IComparable)
        {
            result 
=  ((IComparable)xValue).CompareTo(yValue);
        }
        
//  If values don't implement IComparer but are equivalent
         else   if  (xValue.Equals(yValue))
        {
            result 
=   0 ;
        }
        
//  Values don't implement IComparer and are not equivalent, so compare as string values
         else  result  =  xValue.ToString().CompareTo(yValue.ToString());

        
//  Return result
         return  result;
    }

    
private   int  CompareDescending( object  xValue,  object  yValue)
    {
        
//  Return result adjusted for ascending or descending sort order ie
        
//  multiplied by 1 for ascending or -1 for descending
         return  CompareAscending(xValue, yValue)  *   - 1 ;
    }

    
private   object  GetPropertyValue(T value,  string  property)
    {
        
//  Get property
        PropertyInfo propertyInfo  =  value.GetType().GetProperty(property);

        
//  Return value
         return  propertyInfo.GetValue(value,  null );
    }
}

default14.aspx

 

<% @ Page Language = " C# "  AutoEventWireup = " true "  Trace = " true "  CodeFile = " Default14.aspx.cs "  Inherits = " Default14 "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
    
    
< asp:Repeater ID = " rpt "  runat = " server " >
    
< HeaderTemplate >< table >< tr >< td >< asp:LinkButton ID = " lbtnID "  runat = " server "  Text = " 编号 "  CommandName = " sortby "  CommandArgument = " Id "   /></ td >< td >< asp:LinkButton ID = " lbtnName "  runat = " server "  Text = " 名称 "  CommandName = " sortby "  CommandArgument = " Name "   /></ td ></ tr ></ HeaderTemplate >
    
< ItemTemplate >
    
< tr >< td ><% #DataBinder.Eval(Container.DataItem, " Id " %></ td >< td ><% #DataBinder.Eval(Container.DataItem, " Content " %></ td ></ tr >
    
</ ItemTemplate >
    
< FooterTemplate ></ table ></ FooterTemplate >
    
</ asp:Repeater >
    
</ div >
    
</ form >
</ body >
</ html >

default14.aspx.cs

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Collections.Generic;
using  System.ComponentModel;

public   partial   class  Default14 : System.Web.UI.Page
{

   
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
this.rpt.ItemCommand += new RepeaterCommandEventHandler(rpt_ItemCommand);
        List
<NewsInfo> lstNews=new List<NewsInfo>();
        lstNews.Add(
new NewsInfo(7,"新闻内容8",System.DateTime.Now));
        lstNews.Add(
new NewsInfo(2,"新闻内容2",System.DateTime.Now));
        lstNews.Add(
new NewsInfo(3,"新闻内容6",System.DateTime.Now));
        lstNews.Add(
new NewsInfo(4,"新闻内容4",System.DateTime.Now));
        lstNews.Add(
new NewsInfo(5,"新闻内容5",System.DateTime.Now));
        lstNews.Sort(
new PropertyComparer<NewsInfo>(TypeDescriptor.GetProperties(typeof(NewsInfo)).Find("Id",true), ListSortDirection.Ascending));
        
this.rpt.DataSource=lstNews;
        
this.rpt.DataBind();        
    }


    
void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
    
{

    }

}

NewsInfo.cs

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

/// <summary>
/// News 的摘要说明
/// </summary>

public   class  NewsInfo
{
    
private int _id;
    
private string _content;
    
private DateTime _inTime;
    
public int Id
    
{
        
get return _id; }
        
set { _id = value; }
    }

    
public string Content
    
{
        
get return _content; }
        
set { _content = value; }
    }

    
public DateTime InTime
    
{
        
get return _inTime; }
        
set { _inTime = value; }
    }

    
public NewsInfo() { }
    
public NewsInfo(int id, string content, DateTime inTime)
    
{
        _id 
= id;
        _content 
= content;
        _inTime 
= inTime;
    }

}

 

 

1.http://www.codeproject.com/csharp/ASPNet_Sorting.asp ,翻译见http://blog.csdn.net/veryhappy/archive/2007/02/17/1511361.aspx

2。泛型排序:http://topic.csdn.net/u/20070924/09/f46df595-f551-4838-a610-030f8432996d.html

 http://topic.csdn.net/u/20070914/17/9a27e63a-cad6-4955-8e14-61dc9357a8ad.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值