当前位置:编程学习 > C#/ASP.NET >>

GridView中的分页

我在GridView中建立了分页的模块,如图:

[img=http://hi.csdn.net/space-3604927-do-album-picid-797835.html][/img]

代码如下:

protected void btnFirst_Click(object sender, EventArgs e)
        {
            switch (((LinkButton)sender).CommandArgument.ToString())
            {
                case "first":
                    dgdShowTest.PageIndex = 0;
                    break;
                case "last":
                    dgdShowTest.PageIndex = dgdShowTest.PageCount - 1;
                    break;
                case "prev":
                    dgdShowTest.PageIndex = dgdShowTest.PageIndex - 1;  //显示错误
                    break;
                case "next":
                    dgdShowTest.PageIndex = dgdShowTest.PageIndex + 1;
                    break;
                case "go":
                    {
                        GridViewRow gvr = dgdShowTest.BottomPagerRow;
                        TextBox temp = (TextBox)gvr.FindControl("txtNewPageIndex");
                        int res = Convert.ToInt32(temp.Text.ToString());
                        dgdShowTest.PageIndex = res - 1;
                    }
                    break;
            }
            DataBind();
        }

实现时出现了几个错误/问题:

1、当按上一页时:指定的参数已超出有效值的范围。  参数名: value 。异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。源错误是上面标明的语句

2、按首页、下一页、尾页、go都无数据显示(表格是不止一页的)

请各位大虾指教!谢谢! --------------------编程问答-------------------- 分页

自定义分业

vsiable


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {


       

    }
    public void bind()
    {
        SqlConnection cn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");
        cn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from authors", cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        GridView1.DataSource = ds;
        GridView1.AllowPaging = true;
        GridView1.PageSize = 5;
        GridView1.DataBind();
        if (GridView1.PageIndex == 0)
        {
            Button1.Enabled = false;
            Button2.Enabled = false;
        }
        else
        {
            Button1.Enabled = true;
            Button2.Enabled = true;
        }
        if (GridView1.PageIndex == GridView1.PageCount - 1)
        {
            Button3.Enabled = false;
            Button4.Enabled = false;
        }
        else
        {
            Button3.Enabled = true;
            Button4.Enabled = true;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      
        GridView1.PageIndex = 0;
        bind();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        GridView1.PageIndex = GridView1.PageIndex + 1;
        bind();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        GridView1.PageIndex = GridView1.PageCount - 1;
        bind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.PageIndex = GridView1.PageIndex - 1;
        bind();
    }
}















补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,