Copy Code code as follows:
-----------------------------htmlpage1.htm---------------------------------
<title></title>
<style type= "Text/css" >
table{border:solid 1px #444; background-color:aqua;}
Table Td{border:solid 1px #444;}
</style>
<script src= "Js/jquery1.7.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
var pageindex = 1;
var pagesize = 10;
var lastpageindex = 1;
LoadData ();
function LoadData () {
$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: "Webservice1.asmx/getlistajax",
Data: "{pagesize:" + pagesize + ", pageindex:" + pageindex + "}",
Success:function (Result) {
var strtable = ' <table> ';
Strtable + = ' <tr><td> number </td><td> title </td><td> content </td><td> creation time </ Td></tr> ';
for (var i = 0; i < result.d.length; i++) {
Strtable + = ' <tr> ';
Strtable + = ' <td> ' + result.d[i]. Id + ' </td> ';
Strtable + = ' <td> ' + result.d[i]. Newstitle + ' </td> ';
Strtable + = ' <td> ' + result.d[i]. Newscontent + ' </td> ';
Strtable + = ' <td> ' + result.d[i]. Createtime + ' </td> ';
Strtable + = ' </tr> ';
}
Strtable + = ' </table> ';
$ (' #mydiv '). HTML (strtable);
}
})
}
$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: "Webservice1.asmx/getlastpageindex",
Data: "{pagesize:" + pagesize + "}",
Success:function (Result) {
Lastpageindex = RESULT.D;
}
})
First page
$ (' A:first '). Click (function () {
pageindex = 1;
LoadData ();
})
Previous page
$ (' #divfenye a:eq (1) '). Click (function () {
if (pageindex > 1) {
pageindex--;
LoadData ();
}
})
Next page
$ (' #divfenye a:eq (2) '). Click (function () {
if (pageindex < Lastpageindex) {
pageindex++;
LoadData ();
}
})
Last page
$ (' #divfenye a:eq (3) '). Click (function () {
pageindex = Lastpageindex;
LoadData ();
})
$ (' #divfenye a:last '). Click (function () {
pageindex = $ (' #txtPageindex '). Val ();
LoadData ();
})
$ (' #txtPageindex '). focus (function () {
$ (this). Val (');
})
})
</script>
<body>
<div id= "Mydiv" >
</div>
<div id= "Divfenye" ><a href= "#" > First page </a><a href= "#" > Prev </a><a href= "#" > next page </a ><a href= "#" > Last page </a><input
Id= "Txtpageindex" type= "text"/><a href= "#" >Go</a></div>
</body>
-------------------------WebService1--------------------------------
To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.
[System.Web.Script.Services.ScriptService]
public class WebService1:System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld ()
{
Return to "Hello World";
}
[WebMethod]
Public list<model.t_news1> getlistajax (int pagesize, int pageindex)
{
Bll. T_news1 bnews = new BLL. T_NEWS1 ();
DataTable dt = bnews. Getlistdatatable (pagesize, pageindex);
list<model.t_news1> list = new list<model.t_news1> ();
int Id;
String newstitle = "";
String newscontent = "";
DateTime Createtime;
for (int i = 0; i < dt. Rows.Count; i++)
{
Id = Convert.ToInt32 (dt. rows[i]["Id"]);
Newstitle = dt. rows[i]["Newstitle"]. ToString ();
newscontent = dt. rows[i]["Newscontent"]. ToString ();
Createtime = convert.todatetime (dt. rows[i]["Createtime"]);
MODEL.T_NEWS1 news = new Model.t_news1 ()
{
id = ID,
Newstitle = Newstitle,
Newscontent = Newscontent,
Createtime = Createtime
};
List. ADD (news);
}
return list;
}
[WebMethod]
public int Getlastpageindex (int pagesize)
{
Bll. T_news1 bnews = new BLL. T_NEWS1 ();
int totalcount = Bnews. GetRecordCount ("");
if (totalcount% pagesize = 0)
{
return totalcount/pagesize;
}
Else
{
return totalcount/pagesize + 1;
}
}
------------------------------dal layer:--------------------------
<summary>
Paging Get Data List
</summary>
Public DataTable getlistdatatable (int PageSize, int PageIndex)
{
sqlparameter[] Parameters = {
New SqlParameter ("@PageSize", SqlDbType.Int),
New SqlParameter ("@PageIndex", SqlDbType.Int)
};
Parameters[0]. Value = PageSize;
PARAMETERS[1]. Value = PageIndex;
Return dbhelpersql.runproceduredatatable ("Pro_fenye", parameters);
}
--------------------BLL Layer:--------------------------
Public DataTable getlistdatatable (int pagesize, int pageindex)
{
Return DAL. Getlistdatatable (pagesize, pageindex);
}
------------------Dbhelpersql:-----------------------
public static DataTable runproceduredatatable (String storedprocname, idataparameter[] parameters)
{
using (SqlConnection connection = new SqlConnection (connectionString))
{
DataTable dt = new DataTable ();
Connection. Open ();
SqlDataAdapter SqlDA = new SqlDataAdapter ();
Sqlda.selectcommand = Buildquerycommand (connection, storedprocname, parameters);
Sqlda.fill (DT);
Connection. Close ();
return DT;
}
}