前台部分
<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="USEsp_page.aspx.cs" Inherits="12_7" %>
<!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"> <Columns> <asp:BoundField DataField="sid" 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="btnfirst" runat="server" Text="第一页" Xοnclick="btnfirst_Click" /> <asp:Button ID="btnprov" runat="server" Text="前一页" Xοnclick="btnprov_Click" /> <asp:Button ID="btnnext" runat="server" Text="下一页" Xοnclick="btnnext_Click" /> <asp:Button ID="btnlast" runat="server" Text="最后一页" Xοnclick="btnlast_Click" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:TextBox ID="txtpagenumber" runat="server" Width="19px"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtpagenumber" ErrorMessage="请输入数字" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator> <asp:Button ID="btngo" runat="server" Text="GO" Xοnclick="btngo_Click" /> <br /> <asp:HiddenField ID="HiddenField1" runat="server" /> <asp:HiddenField ID="HiddenField2" runat="server" /> </div> </form></body></html>后台部分
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;
namespace 12_7{ public partial class USEsp_page : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { int pageIndex = 1; BindStudent(pageIndex); } }
private void BindStudent(int pageIndex) { string constr = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = con.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; string sp_name = "sp_Student_Select_by_Page_rowNumber"; cmd.CommandText = sp_name;
cmd.Parameters.AddWithValue("@pageSize", 3); SqlParameter p2 = new SqlParameter("@pageCount", System.Data.SqlDbType.Int); //p2.Direction = ParameterDirection.Output; //cmd.Parameters.Add(p2); //cmd.Parameters.AddWithValue("@pageIndex", pageIndex); cmd.Parameters.Add("@pageCount", System.Data.SqlDbType.Int).Direction = ParameterDirection.Output; cmd.Parameters.AddWithValue("@pageIndex", pageIndex); SqlDataAdapter adpter = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); adpter.Fill(dt); this.GridView1.DataSource = dt; this.DataBind(); int pageCount=Convert.ToInt32 (cmd.Parameters["@pageCount"].Value); this.HiddenField1.Value = pageCount.ToString(); this.HiddenField2.Value = pageIndex.ToString(); } } }
protected void btnfirst_Click(object sender, EventArgs e) { this.BindStudent(1); }
protected void btnlast_Click(object sender, EventArgs e) { this.BindStudent(Convert.ToInt32(this.HiddenField1.Value)); }
protected void btnprov_Click(object sender, EventArgs e) { int index = Convert.ToInt32(this.HiddenField2.Value); if (index > 1) { index--; } this.BindStudent(index); }
protected void btnnext_Click(object sender, EventArgs e) { int pagecount = Convert.ToInt32(this.HiddenField1.Value); int index = Convert.ToInt32(this.HiddenField2.Value); if (index < pagecount) { index++; } this.BindStudent(index); }
protected void btngo_Click(object sender, EventArgs e) { this.BindStudent(Convert.ToInt32(txtpagenumber.Text)); } }}
转载于:https://www.cnblogs.com/qzc900809/archive/2012/12/07/2807933.html