09
2012
04

C# GridView如何分页代码

以下是分页的各个按钮的代码,比如说页面是Order.aspx,那么以下就是Order.aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Securi


以下是分页的各个按钮的代码,比如说页面是Order.aspx,那么以下就是Order.aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class admin_MainOrder
{
protected static int iPageCount =0;//所有记录一共分成几页,这个是系统自动算出来的
protected static int curPage = 1;//当前显示第几页
protected static bool IsFirst = false;//当前页是否是首页
protected static bool IsLast = false;//当前页是否是尾页
protected static int iPageSize = 10;//每页显示几条
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindControls();
}
void BindControls()
{
if (Request.QueryString["Page"] != null)
curPage = Convert.ToInt32(Request.QueryString["Page"]);
else
curPage = 1;

string SQL = "SELECT Q1.*,(SELECT Name FROM Hard WHERE ID=Q1.Q_Hard) AS Q_Hard1,"+
"(SELECT TypeName FROM QuestionType WHERE ID=Q1.Q_Type) AS Q_Type1,"+
"(SELECT TypeName FROM SortType WHERE ID=Q1.Q_Sort) AS Q_Sort1 FROM Questions Q1";
DataView odv=new DataView();
odv = DBFun.CreateAccessView(SQL);

//分页 PagedDataSource这个对象是系统自定义的,直接调用就可以实现分页了
PagedDataSource opds = new PagedDataSource();
opds.DataSource = odv; //获取数据源
opds.AllowPaging = true; //是否在数据绑定中启用分页
opds.PageSize = iPageSize; //每页显示几条
opds.CurrentPageIndex = curPage - 1;//获取当前页索引,这句必须加上
lblCurrentPage.Text = "第" + curPage.ToString() + "页/共"+opds.PageCount+"页 ";
iPageCount = opds.PageCount;//共几页系统自动算出来
IsFirst = opds.IsFirstPage;
IsLast = opds.IsLastPage;
gridQuestion.DataSource = opds;//gridQuestion是GridView名
gridQuestion.DataBind();//绑定
}
//跳转
protected void lnkGoTo_Click(object sender, EventArgs e)
{
if (txtPage.Text.Trim() == "")
return;
string sAoto = txtPage.Text.Trim().ToString();
if (!Fun.IsInteger(sAoto))
return;
if (Convert.ToInt32(sAoto) > 0 && Convert.ToInt32(sAoto) <= iPageCount)
{
string slnkGoto = Request.CurrentExecutionFilePath + "?Page=" + sAoto;
Response.Redirect(slnkGoto);
}
}
//更新
protected void lnkUpdate_Click(object sender, EventArgs e)
{
if (txtShow.Text.Trim() == "")
return;
string sAoto = txtShow.Text.Trim().ToString();
if (!Fun.IsInteger(sAoto))
return;
if (Convert.ToInt32(sAoto)>0 && Convert.ToInt32(sAoto) < 51)
{
iPageSize = Convert.ToInt32(sAoto);
Response.Redirect(Request.CurrentExecutionFilePath);
}
}
//首页
protected void lnkFirst_Click(object sender, EventArgs e)
{
if (iPageCount != 0)
{
string slnk = Request.CurrentExecutionFilePath;
Response.Redirect(slnk);
}
}
//上一页
protected void lnkPrev_Click(object sender, EventArgs e)
{
if (iPageCount != 0 && !IsFirst)
{
string slnk = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage-1);
Response.Redirect(slnk);
}
}
//下一页
protected void lnkNext_Click(object sender, EventArgs e)
{
if (iPageCount != 0 && !IsLast)
{
string slnk = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage +1);
Response.Redirect(slnk);
}
}
//末页
protected void lnkLast_Click(object sender, EventArgs e)
{
if (iPageCount != 0)
{
string slnk = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(iPageCount);
Response.Redirect(slnk);
}
}

}

前面的序号没有填上,我这里补充下
为gridQuestion 控件DataBound 添加AddOrderId事件
protected void AddOrderId(object sender, EventArgs e)
{
for (int i = 0; i < gridQuestion.Rows.Count; i++)
{
//int OrderID = gridQuestion.Rows.RowIndex+1;
int OrderID = curPage * iPageSize - iPageSize + 1;
gridQuestion.Rows.Cells[0].Text = (OrderID + i).ToString();
}
}
« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。