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();
}
}
}
}
分享到:
相关推荐
改进gridView的单选和多选功能 实现全选和取消全选功能
gridview 全选和取消,使用全选进行删除什么的
gridview编辑删除,checkbox单选全选 数据库自行配置
详细介绍了该功能的实现,并带有效果图和Form 页面 代码详细 ,可直接Copy
GridView客户端与服务器端全选方法合集
C#_winform_GridView_CheckBox_多选_全选
GridView js实现全选 (分页保持选择状态) GridView js实现全选 (分页保持选择状态)
gridview存储过程+分页1
GridView 第一列CheckBox全选!postBack和JS方式
GridView查询排序分页GridView查询排序分页GridView查询排序分页
asp.net分页,存储过程,gridview分页,datagrid分页
asp.net GridView中的checkbox实现前台全选或取消全选
GridView无代码分页排序
* GridView通过div分页 * 将数据一次性加载到页面 * 减少访问服务器频率 * checkbox全选
asp.net中的GridView自定义分页及排序代码,对GridView控件进行扩展,实现效果不错的自定义分页及排序代码,供大家学习参考!
ASP.NET GridView中两种全选方法.txt
gridview 编辑 修改 删除 排序 自动加列 背景色 excel表//griview根据listbox自动加列显示列 花了好长时间才全部实现
gridview超简单实现分页及排序功能
实现了VS2005(ASP.NET+C#)下的GridView 客户端排序和分页,汇总. 完成的主要的功能 1.客户端分页 和排序(包括按页排序,按全部数据排序) 2.客户端汇总(包括页汇总,全部汇总) 3.客户端分页( 有数字按钮形式,...
GridView全选与批量删除所使用到的技术: Asp.net技术及数据源控件GridView且使用到Linq技术