Using Linq+jquery+ajax to realize asynchronous paging function simplifies bandwidth pressure _ajax correlation

Source: Internet
Author: User
When the web is displayed, we often encounter paging display, but the online paging method is too much bandwidth, so I thought of using Ajax to paging, using the returned JSON to process the returned data, greatly simplifying the bandwidth pressure. First of all, the idea is to asynchronously perform Ajax to return the data required by the new list in JSON format, output table, you can output the UI Li (High output efficiency) on the page.

Effect Chart:

HTML code:
Copy Code code as follows:

Set their class = "page" to make it easier to add Click event Action pagination
<div id= "ShowPage" style= "width:650px; margin:0 Auto; Display:none "class=" pages >
<div style= "Float:left" >
<a id= "First" class= "pages" > Home </a>
<a id= "prev" class= "pages" > prev </a>
<a id= "Next" class= "pages" > next page </a>
<a id= "Last" class= "pages" > End </a>
Jump to the first <input type= "text" id= "Txtgopage" style= "width:45px; height:15px; border:1px solid "/>
Page
</div>
<div style= "margin:0; Float:left ">
<input type= "button" class= "pages btn btn-info" id= "Go" value= "Jump"/>
A total of <span id= "Sumcount" ></span> data, each page <span id= "ItemCount" ></span> Bar,
Current <span id= "Index" ></span>/<span id= "PageCount" ></span> page
</div>
</div>
The result returned with the following Div output
<div id= "Divbadproductinfo" ></div>

CSS code:
Copy Code code as follows:

* * Pagination/* page
. pages {
Cursor:pointer;
Text-align:center;
margin:0 Auto;
padding-right:0px;
padding-bottom:2px;
padding-top:2px;
Font-family:verdana, Helvetica, Arial, Sans-serif;
}

. pages a {
BORDER-RIGHT:1PX solid;
padding-right:6px;
BORDER-TOP:1PX solid;
padding-left:6px;
padding-bottom:0px;
Overflow:hidden;
BORDER-LEFT:1PX solid;
line-height:20px;
margin-right:2px;
padding-top:0px;
BORDER-BOTTOM:1PX solid;
height:30px;
}

. pages a {
Border-left-color: #e6e7e1;
Border-bottom-color: #e6e7e1;
Color: #09c;
Border-top-color: #e6e7e1;
Background-color: #fff;
Border-right-color: #e6e7e1;
}

. pages A:hover {
Text-decoration:none;
Border-left-color: #09c;
Border-bottom-color: #09c;
Border-top-color: #09c;
Border-right-color: #09c;
}

. pages A.next {
Border-left-color: #09c;
Border-bottom-color: #09c;
Border-top-color: #09c;
Border-right-color: #09c;
}

JS Code:

Introduction: <script src= "Assets/js/jquery-1.8.2.min.js" ></script>//can be used for other versions
Copy Code code as follows:

$ (document). Ready (function ()
{
Retrieval criteria
var search = $ ("#txtFactroy"). Val () + "_" + $ ("#txtTimeSelect"). Val ()
+ "_" + $ ("#txtPinfan"). Val () + "_" +
$ (' input[type= ' checkbox '][name= ' option1 ']:checked '). Val ();
$.ajax ({
Type: "Post", <span style= "color: #ff0000;" >//postback format
URL: "Responsehandler.ashx"//back to the general handler in the processing//return parameters to indicate the request is the first page, encodeuricomponent format Chinese to prevent garbled
Data: "badproductwhere=" + encodeuricomponent (search) + "&currpage=1",
DataType: "JSON",//Bring back the data JSON
async:false,//prohibit using browser caching
Success:function (Returndata, Textstatus, XMLHttpRequest)
{
$ ("#showPage"). CSS (' Display ', ' block ');//Show pagination
$ ("#divBadProductInfo"). HTML (Returndata.split ('_') [0]);//return value split display
var page = Returndata.split ('_') [1].split (', ');
$ ("#SumCount"). Text (page[0]);//total number of data
$ ("#ItemCount"). Text (page[1]);//How many data per page
$ ("#Index"). Text (page[2]);//Current page
$ ("#PageCount"). Text (page[3]);//Total number of pages}
});
Clear the Turn page
$ ("#txtGoPage"). Val ("");

Paging Action action
$ (". Pages"). Click (function () {
The top and bottom last page is available when the total page is greater than 1
if (parsefloat ("#PageCount"). html ()) > 1) {
Gets whether the control type is ID or class
var type = $ (this). attr ("id");
Get how many pages are currently
var Thisindex = $ ("#Index"). Text ();
var search = $ ("#txtFactroy"). Val () + "_" + $ ("#txtTimeSelect"). Val ()
+ "_" + $ ("#txtPinfan"). Val () + "_" +
$ (' input[type= ' checkbox '][name= ' option1 ']:checked '). Val ();
Switch (type) {
Case ' a ':
{
$ ("#txtGoPage"). Val ("");
Badpageindex = 1;
Badpageindex (1, search);//ajax return function
Return
}
Case ' prev ':
{
$ ("#txtGoPage"). Val ("");
Badpageindex = parseint (thisindex)-1;
if (Badpageindex < 1) return;
Badpageindex (Badpageindex, search);
Return
}
Case ' Next ':
{
$ ("#txtGoPage"). Val ("");
Badpageindex = parseint (thisindex) + 1;
if (Badpageindex > parseint ($ ("#PageCount"). HTML ()) return;
Else
Badpageindex (Badpageindex, search);
Return
}
Case ' last ':
{
var max = parseint ($ ("#PageCount"). html ());
$ ("#txtGoPage"). Val ("");
Badpageindex = max;
Badpageindex (max, search);
Return
}
Case ' Go ':
{
var _go = $ ("#txtGoPage"). Val ();
Badpageindex = _go;
Badpageindex (_go, search);
Return
}
}
}
})
});

Copy Code code as follows:

var Badpageindex;
Index, page index such as 1,2,3
Badproductwhere Query criteria
function Badpageindex (index, searchwhere) {
$.ajax ({
Type: "Post",
URL: "Responsehandler.ashx",
Data: "badproductwhere=" + encodeuricomponent (searchwhere) + "&currpage=" + Index,
DataType: "JSON",
Async:false,
Success:function (Returndata, Textstatus, XMLHttpRequest) {
$ ("#divDisplay"). CSS (' Display ', ' none ');
$ ("#showPage"). CSS (' Display ', ' block ');
$ ("#divBadProductInfo"). HTML (Returndata.split ('_') [0]);
var page = Returndata.split ('_') [1].split (', ');
$ ("#SumCount"). Text (page[0]);
$ ("#ItemCount"). Text (page[1]);
$ ("#Index"). Text (page[2]);
$ ("#PageCount"). Text (page[3]);
},
Error:function () {
Alert ("Service error");
}
});

}

C # code: (RESPONSEHANDLER.ASHX)
Copy Code code as follows:

<summary>
Show number of bars per page
</summary>
private int pageSize = 20;
StringBuilder sbbadproductinfo = new StringBuilder ();
if (!string. IsNullOrEmpty (context. request["Badproductwhere"]) &&
!string. IsNullOrEmpty (context. request["Currpage"])
{
#region//b product title information
Sbbadproductinfo.append (@ "<div class= ' Row-fluid ' >
<div class= ' Span12 widget ' ><div class= ' Widget-header ' >
<span class= ' title ' >
<i class= ' icol-blog ' ></i>b product box information
</span>
</div> ");
Sbbadproductinfo.append (@ "<div class= ' widget-content summary-list ' >
<div class= ' row-fluid ' style= ' padding-top:2px; >
<div class= ' span12 section ' >
<table class= ' table table-bordered table-striped ' >
<thead>
<tr>
<th> Case List Number </th>
<th> Boxing Build Date </th>
<th> Factory name </th>
<th> Boxed Products </th>
<th> Packing Box Number </th>
<th> number of packing pieces </th>
<th> Warehouse Confirmation </th>
<th> out of the library confirmation person </th>
<th> out Date </th>
<th> View Details </th>
</tr>
</thead><tbody> ");
#endregion
List<bstate_view> lstgetbadproductdata = (from P in Lstgetbadproductdata
where!string. IsNullOrEmpty (p. Out confirmation) && p. Out Date!= null
Select P). Tolist<bstate_view> ();
String pageInfo = Lstgetbadproductdata.count () + "," + PageSize + "," + context. request["Currpage"] +
"," + (Lstgetbadproductdata.count ()% 20 = 0? (Lstgetbadproductdata.count ()/20):
(Lstgetbadproductdata.count ()/20 + 1));
List<bstate_view> lstgetbaditemdata = (Lstgetbadproductdata.count > pageSize?
Lstgetbadproductdata.skip (int. Parse (context. request["currpage"] = = 1? 0:
PageSize * (int. Parse (context. request["Currpage"]) (1)). Take (PageSize)
: Lstgetbadproductdata). Tolist<bstate_view> ();
#region ==>b Product box information
foreach (var item in Lstgetbaditemdata)
{
var cssname = rowcount% 2 = 0? "Warning": "Error";
Sbbadproductinfo.append (@ "<tr class=" + cssname + "' >");
Sbbadproductinfo.append (@ "<td>" + Item. Box single number + "</td>");
Sbbadproductinfo.append (@ "<td>" + Item. Boxing Build Date + "</td>");
Sbbadproductinfo.append (@ "<td>" + Item. Factory name + "</td>");
Sbbadproductinfo.append (@ "<td>" + item. Packing items + "</td>");
Sbbadproductinfo.append (@ "<td>" + Item. Packing Box number + "</td>");
Sbbadproductinfo.append (@ "<td>" + item. Number of pieces of packing + "</td>");
Sbbadproductinfo.append (@ "<td>" + Item. Warehouse Confirmation + "</td>");
Sbbadproductinfo.append (@ "<td>" + item. Out of the library confirmed person + "</td>");
Sbbadproductinfo.append (@ "<td>" + item. Out Date + "</td>");
Sbbadproductinfo.append (@ "<td><input type= ' button ' class= ' btn btn-primary ')
Style= ' width:80px; height:30px ' value= ' View Details ' ");
Sbbadproductinfo.append (@ "onclick= ' openbadinfo" + Item. Box number + "); ' /></td></tr> ");
rowcount++;
}
Sbbadproductinfo.append (@ "</tbody></table>
</div>
</div>
</div>
</div>
</div>
</div>
</div> ");
#endregion

Context. Response.Write (sbbadproductinfo.tostring () + "_" + pageInfo);
Context. Response.End ();

Paging Effect:


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.