GridView编辑,更新,取消,排序,全选及存储过程分页

1. GridView的分页(2种方法)
a) 内置分页
b) 存储过程分页
2. GridView的排序

3. GridView的全选和全不选

存储过程分页:

存储过程:

USE [student]
GO
/****** Object:  StoredProcedure [dbo].[sp_Student_Select_by_Page_rowNumber]    Script Date: 11/24/2011 22:25:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        lai
-- Create date: 2010-10-3
-- Description:    使用sql2005新增功能rownumber函数完成高效分页
ALTER PROCEDURE [dbo].[sp_Student_Select_by_Page_rowNumber]
    @pageSize int,  --每页记录数量
    @pageCount int output,  --总页数
    @pageIndex int  --当前页索引号

    
AS
BEGIN
declare @totalRecords int
select @totalRecords = count(sid) from student
if(@totalRecords % @pageSize = 0)
    set @pageCount = @totalRecords / @pageSize;
else
    set @pageCount = @totalRecords / @pageSize +1;
with temp as (select row_number() over (order by sid) as id,* from student)
select * from temp where id between (@pageIndex -1)*@pageSize +1 and @pageIndex * @pageSize
return @totalRecords
end

后台代码:

public partial class Default2 : System.Web.UI.Page
{
    string str = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindStudent(1);
        }
    }

private void BindStudent(int pageIndex)
{
        using (SqlConnection sqlCnn=new SqlConnection(str))
        {
            using (SqlCommand sqlCmm=sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "sp_Student_Select_by_Page_rowNumber";
                sqlCmm.CommandType = CommandType.StoredProcedure;
                sqlCmm.Parameters.AddWithValue("@pageSize",3);
                sqlCmm.Parameters.Add("@pageCount",SqlDbType.Int).Direction=ParameterDirection.Output;
                sqlCmm.Parameters.AddWithValue("@pageIndex",pageIndex);

                SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                this.GridView1.DataSource = ds.Tables[0];
                this.GridView1.DataBind();
                this.HiddenField1.Value = pageIndex.ToString();
                this.HiddenField2.Value = sqlCmm.Parameters["@pageCount"].Value.ToString();
                this.Label1.Text = pageIndex + "/" + this.HiddenField2.Value;
            }
        }
}
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.BindStudent(1);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(this.HiddenField1.Value);
        if (index>1)
        {
            index--;
        }
        this.BindStudent(index);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(this.HiddenField1.Value);
        int total = Convert.ToInt32(this.HiddenField2.Value);
        if (index<total)
        {
            index++;
        }
        this.BindStudent(index);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        int total = Convert.ToInt32(this.HiddenField2.Value);
        this.BindStudent(total);
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        int total = Convert.ToInt32(this.HiddenField2.Value);
        int page =Convert.ToInt32(this.TextBox1.Text);
        if (page <= total)
        {
            this.BindStudent(page);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(),"提示","<script>alert('对不起,没有您要查找的页数')</script>",false);
        }
    }


编辑:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindStudent(Convert.ToInt32(HiddenField1.Value));

    }

更新:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       string connStr = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
       TextBox Name = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
       HiddenField id = GridView1.Rows[e.RowIndex].FindControl("HiddenField3") as HiddenField;

       string SqlStr = "update student set sname='" + Name.Text + "' where sid='"+id.Value+"'";

       try
       {
           SqlConnection conn = new SqlConnection(connStr);
           if (conn.State.ToString() == "Closed") conn.Open();
           SqlCommand comm = new SqlCommand(SqlStr, conn);
           comm.ExecuteNonQuery();
           comm.Dispose();
           if (conn.State.ToString() == "Open") conn.Close();

         
       }
       catch (Exception ex)
       {
           Response.Write("数据库错误,错误原因:" + ex.Message);
           Response.End();
       }
       GridView1.EditIndex = -1;
       BindStudent(Convert.ToInt16(HiddenField1.Value));
    }

取消:
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindStudent(Convert.ToInt16(HiddenField1.Value));
    }

全选:使用html控件

function change(sender) {
            var table = document.getElementById("GridView1");
            for (var i = 1; i < table.rows.length; i++) {
                table.rows[i].cells[0].getElementsByTagName("input")[0].checked = sender.checked;
            }

 <input id="Checkbox1" type="checkbox" onclick="change(this)" />

排序:

首先把标题的SortExpresstion属性修改为绑定字段

然后把GridView的Allow属性改为true

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

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindStudent("sid");
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        this.BindStudent(e.SortExpression);
    }

    private void BindStudent(string sort)
    {
        string str = "server=.\\sqlexpress;database=student;integrated security=sspi";
        using (SqlConnection sqlCnn = new SqlConnection(str))
        {
            using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "select * from student order by " + sort;
                SqlDataAdapter da = new SqlDataAdapter(sqlCmm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.GridView1.DataSource = ds.Tables[0];
                this.GridView1.DataBind();
            }

        }
    }
}


你可能感兴趣的:(object,String,存储,button,textbox,dataset)