新建TestAjax.aspx
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;//
using System.Data.SqlClient;//
namespace AjaxTest.AjaxPage
{
public partial class TestAjax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//获取每页要显示多少条记录
string strPageSize = Request.QueryString["pagesize"];
int pageSize;
if (strPageSize == null)
{
pageSize = 3;//如果没有获取到参数,则将pageSize默认显示3条记录
}
else
{
pageSize = Convert.ToInt32(strPageSize);//将获取到的参数转换为int型
}
//获取当前页数
string strPageIndex = Request.QueryString["pageindex"];
int pageIndex;
if (strPageIndex == null)
{
pageIndex = 1;//如果没有获取到参数,则将pageIndex默认显示第一页
}
else
{
pageIndex = Convert.ToInt32(strPageIndex);
}
//调用数据库中分页的存储过程
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyBookShop;User ID=sa;password=123");
SqlCommand command = new SqlCommand("books_pager ", conn);//books_pager为分页的存储过程名
command.CommandType = CommandType.StoredProcedure;//更改CommandType的类型
conn.Open();//打开连接
//books_pager此存储过程需要3个参数,实例化3个参数对象
SqlParameter spPageSize = new SqlParameter("@pageSize", pageSize);//每页显示多少条记录
SqlParameter spPageIndex = new SqlParameter("@pageIndex", pageIndex);//当前页
SqlParameter spTotalPages = new SqlParameter("@totalPages", SqlDbType.Int);//总页数,SqlDbType.Int将数据类型转换为int型
spTotalPages.Direction = ParameterDirection.Output;//总页数为输出参数
command.Parameters.Add(spPageSize);
command.Parameters.Add(spPageIndex);
command.Parameters.Add(spTotalPages);
SqlDataReader reader = command