Detailed idea and code for implementing the paging function in AJAX and L3 Architecture

Source: Internet
Author: User

Copy codeThe Code is as follows: -------------------------htmlpage1.htm ---------------------------------
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<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> NO. </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> ';
Comment ('{mydiv'}.html (strtable );
}
})
}
$. Ajax ({
Type: "post ",
ContentType: "application/json ",
Url: "WebService1.asmx/GetLastPageindex ",
Data: "{pagesize:" + pagesize + "}",
Success: function (result ){
Lastpageindex = result. d;
}
})
// Page 1
$ ('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: la'). click (function (){
Pageindex = $ ('# txtpageindex'). val ();
Loaddata ();
})
$ ('# Txtpageindex'). focus (function (){
$ (This). val ('');
})
})
</Script>
</Head>
<Body>
<Div id = "mydiv">
</Div>
<Div id = "divfenye"> <a href = "#"> page 1 </a> <a href = "#"> previous page </a> <a href =" # "> next page </a> <a href =" # "> last page </a> <input
Id = "txtPageindex" type = "text"/> <a href = "#"> Go </a> </div>
</Body>
</Html>
------------------------- WebService1 --------------------------------
// To allow ASP. net ajax to call this Web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. ScriptService]
Public class WebService1: System. Web. Services. WebService
{
[WebMethod]
Public string HelloWorld ()
{
Return "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>
/// Retrieve the data list by PAGE
/// </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;
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.