论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp.net教程
Tag:静态页面,treeview,gridview,repeater,dataset,sqldatareader,ado.net,上传,三层,ajax,xml,留言本,新闻发布,商城,注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,触发器,socket,form认证,登录,视频教程

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

文章类别:Asp.net | 发表日期:2008-10-5 22:12:20

  要想实现翻页后继续排序,实现这样的效果:

  比如共 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)
 {
  //异常
 }
}

视频教程列表
文章教程搜索
 
Asp.net推荐教程
Asp.net热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058