以下是分页的各个按钮的代码,比如说页面是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();
        }
    }
															
          
          09
2012
04
      C# GridView如何分页代码
            发布:郑德才博客 | 分类:学习之路 | 评论:0 | 浏览:
          
        相关文章:
【C#、Asp.Net 工具类大全】GridView 绑定数据及操作大全工具类 (2015-6-14 20:58:34)
Asp.Net下GridView合并TemplateField模板列和BoundField绑定列方法 (2013-12-19 18:5:14)
Gridview使用CheckBox全选与单选 (2013-5-22 22:29:38)
GridView和DataGrid相同列合并 (2012-10-18 22:16:4)
Asp.net中GridView非常详细的使用详解GridView无代码分页排序 (2012-8-1 15:3:44)
GridView自定义的模版分页,简单实现 (2012-7-18 7:36:44)
asp.net 关于获取GridView列的值 (2012-7-8 6:52:9)
Asp.Net GridView每条记录前面都有一个多选框,可以实现全选 (2012-5-4 21:21:23)
查询数据库并绑定到GridView (2012-4-25 22:54:9)
Asp.Net GridView批量删除数据操作 (2012-4-18 7:54:7)
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。