如何让分页和排序共存?

点第二页的话,排序就没有了。要是点排序的话,又回到了第一页。
是不是应该在pageindexchanged中把排序的关键字记下来,在databind。而在sort的时候,把当前页记下来

原来写的 你看看
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/***************************  DataGrid控件的分页和排序   **************/
/*
 * 功能 => DataGrid 控件的分页和排序 利用ViewState存储排序表达式,
 *       
 *        因为ViewState不占服务器资源,所以不放在Session中,但是ViewState是发回給
 *    客户端的,最好放些"小"内容,所以用ViewState存储排序表达式,而 DataView则
 *    用Session 存储
 *
 * 说明 => 排序功能最好放在 DataGrid控件的 Pre_Render事件中
 *         Pre_Render事件是DataGrid控件呈现給用户前所发生的事件
 * 时间 => 2003/6/13   创建人 => stpangpang
 *
*/
/********************************************************************/

namespace ass
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid DataGrid1;

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!Page.IsPostBack)
   {
    //先设定,排序的方向为 ASC 放在 ViewState 中
    ViewState["SortDirection"]="ASC";
    this.BindGrid();
   }
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
   this.DataGrid1.PreRender += new System.EventHandler(this.DataGrid1_PreRender);
   this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void BindGrid()
  {
   SqlConnection conn=new SqlConnection("server=localhost;database=northwind;uid=northwind;pwd=northwind;");
   conn.Open();
   SqlCommand cmd=new SqlCommand("select employeeid,firstname from employees",conn);
   //SqlDataReader reader=cmd.ExecuteReader();
    
   SqlDataAdapter adapter=new SqlDataAdapter("select employeeid ,firstname from employees",conn);
   DataSet DS=new DataSet();
   adapter.Fill(DS);
   Session["DV"]=DS.Tables[0].DefaultView;
   
  }

  private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
  {
   this.DataGrid1.CurrentPageIndex=0;
   //属性SortField 存促排序的字段
   SortField=e.SortExpression;

   //重新绑定DataGrid控件
   BindGrid();
  }

  private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
   
  }

  private void DataGrid1_PreRender(object sender, System.EventArgs e)
  {
   //判断排序方向
   if(ViewState["SortDirection"].ToString()=="ASC")
   {
    //如果是升序就改为降序
    ViewState["SortDirection"]="DESC";
   }
   else
   {
    //如果是降序就改为升序
    ViewState["SortDirection"]="ASC";
   }
   //排序字段是 属性SortField存储的字段
   DataView DV=(DataView)Session["DV"];
   DV.Sort=SortField;
   
   try
   {
    //设定,排序表达式
    DV.Sort=SortField + " " + ViewState["SortDirection"].ToString();
   }
   catch
   {
    //不作任何处理,因为第一次加载,SortField 的属性是空(String.Empty) 会有错误
   }
   this.DataGrid1.DataSource=DV;
   this.DataGrid1.DataBind();

  }

  //属性,SortField 用ViewState跟踪排序字段
  string SortField
  {
   get
   {
    object o=ViewState["SortField"];
   
    if(o==null)
    {
     //如果为空,返回空字符串
     return String.Empty;
    }
    return (string)ViewState["SortField"];
   }
   set
   {
    ViewState["SortField"]=value;
    
   }
  }
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值