Gridview分页代码的例子

发布时间:2019-08-07编辑:脚本学堂
为大家提供一个gridview分页的实例代码,有需要的朋友,可以参考下。

gridview分页的完整代码。
 

复制代码 代码示例:

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.SqlClient;

public partial class gv4 : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             bind();   
         }
       
     }
     private void bind()
     { //建立一个方法,用与在页面中为控件绑定数据
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
         //初始化连接
         SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);
         DataSet ds = new DataSet();
         sda.Fill(ds,"authors");

         GridView1.DataSource = ds.Tables["authors"];

         GridView1.AllowPaging = true;//设置他可以分页,前台必须设置分页模板不可见,否则他要使坏
         GridView1.PageSize = 5;//分页大小为10
         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;
         }
     }
     private int cmd(string query)
     {//页内多次涉及到修改删除等要使用到这个,所以就做成一个方法
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
         //初始化连接
         con.Open();
         SqlCommand cmd = new SqlCommand(query,con);
         return cmd.ExecuteNonQuery();
     }
     protected void Button1_Click(object sender, EventArgs e)
     {//实现分页
         switch (((Button)sender).CommandArgument.ToString())
         {
             case "first":
                 GridView1.PageIndex = 0;
                 break;
             case "last":
                 GridView1.PageIndex = GridView1.PageCount-1;
                 break;
             case "prev":
                 GridView1.PageIndex = GridView1.PageIndex - 1;
                 break;
             case "next":
                 GridView1.PageIndex = GridView1.PageIndex + 1;
                 break;
         }
         bind();
     }
     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
     {//进入编辑模式
         GridView1.EditIndex = e.NewEditIndex;
         bind();
     }
     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {//编辑动作
         string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
         string fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox1")).Text;
         //注意上面,之所以找到了文本框的名字,是因为把所有的列都转换成了模板列
         //但是编辑他的数据根本没有这么麻烦,我们即将学到的DetailsView和FromView可以轻松完成这个任务.
         string city = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox2")).Text;
         string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox3")).Text;
         string query = "update authors set au_fname='" + fname + "',city='" + city + "',phone='"+phone+"' where au_id='" + id + "'";
         if (cmd(query) > 0)
         {
             GridView1.EditIndex = -1;
             bind();
         }
     }
     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
     {//cancel事件
         GridView1.EditIndex = -1;
         bind();
     }
     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
     {//删除事件
         string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
         //得到该行的主键
         string query = "delete from authors where au_id='"+id+"'";
         if (cmd(query) > 0)
         {
             bind();
         }
     }
}