打印本文 打印本文 关闭窗口 关闭窗口
Asp.net中DataGrid控件的自定义分页
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2015  更新时间:2009/4/23 10:45:33  文章录入:mintao  责任编辑:mintao
>

 

     comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));

     comm.Parameters[1].Value = pageSize;

 

     comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));

     comm.Parameters[2].Direction = ParameterDirection.Output;

 

     comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));

     comm.Parameters[3].Direction = ParameterDirection.Output;

 

     comm.CommandType = CommandType.StoredProcedure;

     SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

     DataSet ds = new DataSet();

     dataAdapter.Fill(ds);

 

     recordCount = (int)comm.Parameters[2].Value;

     pageCount = (int)comm.Parameters[3].Value;

 

     return ds;

}

//绑定数据到DataGrid,同时刷新数据总记录数

private void DataGridDataBind()

{

     DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);

     this.DataGrid1.VirtualItemCount = RecordCount;

     this.DataGrid1.DataSource = ds;

     this.DataGrid1.DataBind();

}

下面是分页的几个变量属性

public int PageCount

{

     get{return this.DataGrid1.PageCount;}

}

 

public int PageSize

{

     get{return this.DataGrid1.PageSize;}

}

 

public int PageIndex

{

     get{return this.DataGrid1.CurrentPageIndex;}

     set{this.DataGrid1.CurrentPageIndex = value;}

}

 

public int RecordCount

{

     get{return recordCount;}

}

注册DataGrid分页事件

//分页事件处理

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

     DataGrid dg = (DataGrid)source;

     dg.CurrentPageIndex = e.NewPageIndex;

     DataGridDataBind();

}

最好判断当前页面是否是第一次加载,防止重复加载两次数据,

private void Page_Load(object sender, System.EventArgs e)

{

     if(!Page.IsPostBack)

     {

         DataGridDataBind();

     }

}

显示界面如下:

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口