ASP.NET 翻页后继续维持排序

要想实现翻页后继续排序,实现这样的效果: 比如共 15笔记录,每页显示10条
  则排序时:第一页将前10条记录排序,翻第二页时后五条再单独排序.

  要注意以下几点:

  1.如果数据很多,最好不要把数据集放到缓存中(viewstate),影响性能

  2. viewstate中存放上次是哪个e.expression并且存放此e.expressi

  示例如下:

  1.现有的排序事件是这样写的,这个是点击上面排序标题时用:

private void grdprojtrace_sortcommand(object source, datagridsortcommandeventargs e)
{
 this.grdprojtrace.currentpageindex = 0;
 dataview dv = 得到数据代码;
 string strsort = "";
 string strorder ="";//排序方式。0,降序,1升序
 if(viewstate["sortexpressti != null)
 {
  strsort = viewstate["sortexpresstion"].tostring();
  strsort = strsort.substring(0,strsort.length -1);
  strorder = viewstate["sortexpresstion"].tostring();
  strorder = strorder.substring(strorder.length -1);
 }
 if(e.sortexpression == "customername")
 {
  if(strsort != "customername")
  {
   this.viewstate["sortexpressti = ustomername0";
   dv.sort = "customername desc";
  }
  else
  {
   if(strorder == "0")
   {
    this.viewstate["sortexpressti = "customername1";
    dv.sort = "customername asc";
   }
   else
   {
    this.viewstate["sortexpressti = "customername0";
    dv.sort = "customername desc";
   }
  }
 }
 if(e.sortexpression == "fullname")
 {
  if(strsort != "fullname")
  {
   this.viewstate["sortexpressti = "fullname0";
   dv.sort = "fullname desc";
  }
  else
  {
   if(strorder == "0")
   {
    this.viewstate["sortexpressti = "fullname1";
    dv.sort = "fullname asc";
   }
   else
   {
    this.viewstate["sortexpressti = "fullname0";
    dv.sort = "fullname desc";
   }
  }
 }
 this.grdprojtrace.datasource = dv;
 this.grdprojtrace.databind();
}

  2.下面这个方法是自己写的,翻页事件中调用。

private void changepagedatabind()
{
 dataview dv = 得到数据代码;
 string strsort = "";
 string strorder ="";//排序方式。0,降序,1升序
 if(viewstate["sortexpressti != null)
 {
  strsort = viewstate["sortexpresstion"].tostring();
  strsort = strsort.substring(0,strsort.length -1);
  strorder = viewstate["sortexpresstion"].tostring();
  strorder = strorder.substring(strorder.length -1);
 }
 if(this.viewstate["sortexpressti != null)
 {
  if(strsort == "customername")
  {
   if(strorder == "1")
   {
    this.viewstate["sortexpressti = "customername1";
    dv.sort = "customername asc";
   }
   else
   {
    this.viewstate["sortexpressti = "customername0";
    dv.sort = "customername desc";
   }
  }
 }
 if(this.viewstate["sortexpressti != null)
 {
  if(strsort == "fullname")
  {
   if(strorder == "1")
   {
    this.viewstate["sortexpressti = "fullname1";
    dv.sort = "fullname asc";
   }
   else
   {
    this.viewstate["sortexpressti = "fullname0";
    dv.sort = "fullname desc";
   }
  }
 }
 this.grdprojtrace.datasource = dv;
 this.grdprojtrace.databind();
}

  上面两方法只要修改要排序的字段名,就可以直接调用了.

  1、方法很简单实用,这里就不说了。

  2、方法是这样用的:

private void grdprojtrace_pageindexchanged(object source, datagridpagechangedeventargs e)
{
 try
 {
  try
  {
   this.grdprojtrace.currentpageindex = e.newpageindex;
  }
  catch
  {
   this.grdprojtrace.currentpageindex = 0;
  }

  this.changepagedatabind();
 }
 catch(system.exception errws)
 {
  //异常
 }
}