看帖评论是美德~!~!~!~!
可以根据自己数据库表更改其中数据名!!!!如果需要完整教程,可以联系我!
//前台(aspx)代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cunchuguochengfenye.aspx.cs" Inherits="cunchuguochengfenye" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Height="136px" Width="408px">
<Columns>
<asp:BoundField DataField="id" HeaderText="行号" />
<asp:BoundField DataField="sid" HeaderText="编号" />
<asp:BoundField DataField="sname" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="age" HeaderText="年龄" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="First" />
<asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="Prev" />
<asp:Button ID="Button3" runat="server" οnclick="Button3_Click" Text="Next" />
<asp:Button ID="Button4" runat="server" οnclick="Button4_Click" Text="Last" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
跳转到:<asp:TextBox ID="TextBox1" runat="server" Width="90px"></asp:TextBox>
<asp:Button ID="Button5" runat="server" οnclick="Button5_Click" Text="Go" />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="错!" Operator="DataTypeCheck"
Type="Integer"></asp:CompareValidator>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
</div>
</form>
</body>
</html>
//后台(cs)代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class cunchuguochengfenye : System.Web.UI.Page
{
private string strCnn;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindStudent(1);
}
private void BindStudent(int pageIndex)
{
strCnn = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(strCnn))
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
{
sqlCmm.CommandText = "sp_Student_Select_by_Page_rowNumber"; //存储过程名称
SqlDataAdapter da = new SqlDataAdapter(sqlCmm);
sqlCmm.CommandType = System.Data.CommandType.StoredProcedure; //指定执行的SQL语句为存储过程
sqlCmm.Parameters.AddWithValue("@pageSize", 3);
sqlCmm.Parameters.Add("@pageCount", SqlDbType.Int).Direction = ParameterDirection.Output;
sqlCmm.Parameters.AddWithValue("@pageIndex", pageIndex);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.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 Button4_Click(object sender, EventArgs e)
{
this.BindStudent(Convert.ToInt32(this.HiddenField2.Value));
}
protected void Button2_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField1.Value);
if (index > 0)
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 Button5_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text != "")
{
int total = Convert.ToInt32(this.HiddenField2.Value);
int index = Convert.ToInt32(this.TextBox1.Text);
if (index <= total)
{
this.BindStudent(index);
}
}
else
{
Response.Write("<script language='javacript' type='text/javascript'>");
Response.Write("alert('你想往哪跳啊?');");
Response.Write("</script>");
}
}
}
//存储过程(主要借鉴下写法)
USE [student]
GO
/****** Object: StoredProcedure [dbo].[sp_Student_Select_by_Page_rowNumber] Script Date: 11/24/2011 19:21:58 ******/
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
这样写可以避免使用绑定数据的GridView自带的分页出现的若干问题!具体不这太清楚什么问题,总之会避免就对了!
如果哪位高手了解,可以传授一下下~~~