欢迎光临
我们一直在努力

ASP.Net 翻页后继续维持排序-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

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

要注意以下几点:
1. 把数据集放到缓存中,例如: viewstate
2. viewstate中存放上次是哪个e.expression并且存放此e.expression是升序还是降序

示例如下:
1.现有的排序事件是这样写的,这个是点击上面排序标题时用:
  private void grdprojtrace_sortcommand(object source, datagridsortcommandeventargs e)
{
 this.grdprojtrace.currentpageindex = 0;
 dataview dv = 得到数据代码;
 string strsort = “”;
 string strorder =””;//排序方式。0,降序,1升序
 if(viewstate[“sortexpresstion”] != 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[“sortexpresstion”] = ustomername0″;
   dv.sort = “customername desc”;
  }
  else
  {
   if(strorder == “0”)
   {
    this.viewstate[“sortexpresstion”] = “customername1”;
    dv.sort = “customername asc”;
   }
   else
   {
    this.viewstate[“sortexpresstion”] = “customername0”;
    dv.sort = “customername desc”;
   }
  }
 }
if(e.sortexpression == “fullname”)
 {
  if(strsort != “fullname”)
  {
   this.viewstate[“sortexpresstion”] = “fullname0”;
   dv.sort = “fullname desc”;
  }
  else
  {
   if(strorder == “0”)
   {
    this.viewstate[“sortexpresstion”] = “fullname1”;
    dv.sort = “fullname asc”;
   }
   else
   {
    this.viewstate[“sortexpresstion”] = “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[“sortexpresstion”] != 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[“sortexpresstion”] != null)
 {    
  if(strsort == “customername”)
  {
   if(strorder == “1”)
   {
    this.viewstate[“sortexpresstion”] = “customername1”;
    dv.sort = “customername asc”;
   }
   else
   {
    this.viewstate[“sortexpresstion”] = “customername0”;
    dv.sort = “customername desc”;
   }
  }
 }
 if(this.viewstate[“sortexpresstion”] != null)
 {    
  if(strsort == “fullname”)
  {
   if(strorder == “1”)
   {
    this.viewstate[“sortexpresstion”] = “fullname1”;
    dv.sort = “fullname asc”;
   }
   else
   {
    this.viewstate[“sortexpresstion”] = “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)
 {  
         //异常
 }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.Net 翻页后继续维持排序-.NET教程,Asp.Net开发
分享到: 更多 (0)

相关推荐

  • 暂无文章