<head runat="server">
<title></title>
<script src="Jquery1.7.js" type="text/javascript"></script> //引入Jquery包
<script type="text/javascript">
$(function () {
var pageindex = 0;
WebApplication1.WebService1.GetCount(onSuccess, onFailed);
function onSuccess(result) {
totalpage = parseInt(result);
}
function onFailed() {
alert('调用失败');
}
function GetData() {
WebApplication1.WebService1.DataBindArticle(pageindex, onSuccess1, onFailed1);
}
function onSuccess1(result) {
$('#mydiv').html(result);
}
function onFailed1() {
alert('调用失败');
}
$('#btnfirst').click(function () {
pageindex = 1;
$('#txtpageindex').val(pageindex);
GetData();
})
$('#btnpre').click(function () {
if (pageindex > 1) {
pageindex--;
$('#txtpageindex').val(pageindex);
}
GetData();
})
$('#btnnext').click(function () {
if (pageindex < totalpage) {
pageindex++;
$('#txtpageindex').val(pageindex);
}
GetData();
})
$('#btnlast').click(function () {
pageindex = totalpage;
$('#txtpageindex').val(pageindex);
GetData();
})
$('#btngo').click(function () {
pageindex = $('#txtpageindex').val();
GetData();
})
$('#btnfirst').click();
})</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" /></Services>
</asp:ScriptManager>
<div id=mydiv></div>
<div>
<input type="button" value="首页" id="btnfirst" />
<input type="button" value="下一页" id="btnnext" />
<input type="button" value="上一页" id="btnpre" />
<input type="button" value="末页" id="btnlast" />
<input type="text" id="txtpageindex" />
<input type="button" value="GO" id="btngo" />
</div>
</form>
</body>
</html>
//创建WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Text;
namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
string str = ConfigurationManager.ConnectionStrings["sqlcnn"].ConnectionString; //连接数据库
private int pagesize = 5;
[WebMethod]
public string DataBindArticle(int pageindex)
{
SqlConnection cnn = new SqlConnection(str);
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "procarticle";//存储过程
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pagesize",pagesize);
cmd.Parameters.AddWithValue("@pageindex",pageindex);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
StringBuilder sb1 = new StringBuilder();
sb1.Append("<table boreder=1>");
for (int i = 0; i < dt.Rows.Count; i++)
{
string title = Convert.ToString(dt.Rows[i]["TITLE"]);
string classname = Convert.ToString(dt.Rows[i]["CLASSNAME"]);
string clickcount = Convert.ToString(dt.Rows[i]["CLICKCOUNT"]);
string publisher = Convert.ToString(dt.Rows[i]["PUBLISHER"]);
string published = Convert.ToString(dt.Rows[i]["PUBLISHED"]);
sb1.Append("<tr>");
string str1 = "<td>" + title + "</td>" + "<td>" + classname + "</td>" + "<td>" + clickcount + "</td>" + "<td>" + publisher + "</td>" + "<td>" + published + "</td>";
sb1.Append(str1+"</tr>");
}
sb1.Append("</table>");
cmd.Dispose();
cnn.Close();
cnn.Dispose();
return sb1.ToString();
}
//获取总的页数
[WebMethod]
public string GetCount()
{
int totalpagesize = 0;
SqlConnection cnn = new SqlConnection(str);
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "select count(*) from ArticleInfo";
cnn.Open();
object obj = cmd.ExecuteScalar();
if (obj!=null)
{
int totalrecord = Convert.ToInt32(obj);
if (totalrecord % pagesize != 0)
{
totalpagesize = totalrecord / pagesize + 1;
}
else
{
totalpagesize = totalrecord / pagesize;
}
}
cmd.Dispose();
cnn.Dispose();
return totalpagesize.ToString();
}
}
}
//创建存储过程分页
create proc procarticle
@pageindex int,
@pagesize int
as
select * from(select ROW_NUMBER() over(order by ARTICLEID)as rownumb,* from ArticleInfo)t
left outer join ArticleClass c on t.CLASSID=c.CLASSID
where t.rownumb>(@pageindex-1)*@pagesize and
t.rownumb<=@pageindex*@pagesize