Implementation Code of jquery + JSON without refreshing Paging

Source: Internet
Author: User

Controls Code : Copy code The Code is as follows: using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. LINQ;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. reflection;
Using system. IO;
[Assembly: webresource ("pagebarjs. js", "application/X-JavaScript")]
Namespace hawkon. Control {
[Defaultproperty ("text")]
[Toolboxdata ("<{0}: pagebar runat = Server> </{0}: pagebar>")]
Public class pagebar: webcontrol {
[Bindable (true)]
[Category ("data")]
[Defaultvalue ("1")]
[Localizable (true)]
Public int pageindex {
Get {
Return pageindex;
}
Set {
Pageindex = value;
}
}
Private int pageindex;
[Bindable (true)]
[Category ("data")]
[Defaultvalue ("1")]
[Localizable (true)]
Public int pagecount {
Get {
Return pagecount;
}
Set {
Pagecount = value;
}
}
Private int pagecount;
[Bindable (true)]
[Category ("data")]
[Defaultvalue ("5")]
[Localizable (true)]
Public int displaycount {
Get {
Return displaycount;
}
Set {
Displaycount = value;
}
}
Private int displaycount;
Protected override void rendercontents (htmltextwriter output ){
String html = "";
HTML + = createa (1, "<");
Int B = 0, E = 0;
If (pageindex <= displaycount ){
B = 1;
E = displaycount * 2 + 1;
}
Else if (pageindex> pagecount-displaycount ){
B = pagecount-displaycount * 2;
E = pagecount;
}
Else {
B = pageindex-displaycount;
E = pageindex + displaycount;
}
If (B <= 1 ){
HTML + = createa (1 ,"");
}
Else
HTML + = createa (B-1 ,"");
For (INT I = B; I <= E; I ++ ){
HTML + = createa (I, I. tostring ());
}
HTML + = createa (E + 1 ,"");
HTML + = createa (pagecount, "> ");
// Html + = string. Format ("<SCRIPT src = '{0}' type = 'text/JavaScript '> </SCRIPT> ",
// This. Page. clientscript. getwebresourceurl (typeof (pagebar), "jscript1.js "));
Output. Write (HTML );
}
Private string createa (INT pageindex, string text ){
If (pageindex = This. pageindex ){
Return string. format ("<a class = \" pagea \ "Title = 'page' id = \" {0} \ "> {1} </a>", pageindex, text );
}
Return string. format ("<a class = \" pagea \ "Title = 'page' href = \" # \ "id = \" {0} \ ">{1} </> ", pageindex, text );
}
Protected override void onprerender (eventargs e ){
Base. onprerender (E );
String resourcename = "pagebarjs. js ";
String url = This. Page. clientscript. getwebresourceurl (this. GetType (), "pagebarjs. js ");
String script = "\ r \ n <SCRIPT src = \" "+ httputility. htmlattributeencode (URL) + "\" type = \ "text/JavaScript \"> </SCRIPT> ";
This. Page. clientscript. registerclientscriptblock (this. GetType (), resourcename, script, false );
Script = @ "<SCRIPT type =" "text/JavaScript" ">$ (document ). ready (function () {initpagebar (5, "" booklistbypage "", "" books "", 50, "" pagea "") ;}); </SCRIPT> "; this. page. clientscript. registerclientscriptblock (this. getType (), "ready", script );
}
Protected override void render (htmltextwriter writer ){
Base. Render (writer );
}
}
}

JS resource file code: Copy code The Code is as follows: var displaycount;
VaR getdataurl;
VaR booktableid;
VaR currentindex;
VaR pagecount;
VaR linkclass;
VaR fields;
Function initpagebar (DC, gdu, btid, PC, LC ){
Displaycount = Dc;
Getdataurl = gdu;
Booktableid = btid;
Currentindex = 1;
Pagecount = pc;
Linkclass = "." + Lc;
$ (Linkclass). Click (getpage );
}
Function getpagebyid (ID ){
$ ("# Ci"). Val (ID );
VaR Surl = getdataurl + "? Pageindex = "+ ID;
$. Ajax ({
URL: Surl,
Type: "Get ",
Datatype: "JSON ",
Timeout: 1000,
Success: showresult
}
);
}
Function getpage (){
Getpagebyid ($ (this). Get (0). Id );
}
Function showresult (result ){
For (I = 1; I <= result. Data. length; I ++ ){
VaR id = "#" + booktableid + "TR: Nth-child (" + I + ")";
OBJ = result. Data [I-1];
For (var key in OBJ ){
CTL = $ (ID). Find ("." + key );
If (CTL. length> 0 ){
CTL. Get (0). innerhtml = OBJ [Key];
}
}
}
$ (Linkclass). Each (function (INDEX ){
VaR I, B, E;
If (result. currentpageindex <= displaycount ){
B = 1;
E = (displaycount + 1) * 2;
I = index-2 + 1;
}
Else if (result. currentpageindex> pagecount-displaycount ){
B = pagecount-displaycount * 2;
E = pagecount;
I = pagecount-displaycount * 2 + index-2;
}
Else {
I = result. currentpageindex-displaycount + index-2;
B = result. currentpageindex-displaycount-1;
E = result. currentpageindex + displaycount + 1;
}
If ($ (this). Get (0). Id = $ (this). Text ()){
$ (This). Text (I );
}
Else if (Index = 1 ){
If (B <= 1 ){
$ (This). Get (0). Id = 1;
}
Else {
$ (This). Get (0). Id = B-1;
}
}
Else if (Index = displaycount * 2 + 3 ){
$ (This). Get (0). Id = E;
}
$ (This). ATTR ("href ","#");
If (I> = B) & (I <= E )){
$ (This). Get (0). Id = I;
}
If ($ (this). Text = result. currentpageindex ){
$ (This). removeattr ("href ");
}
});
Currentindex = result. currentpageindex;
}

HTML page code: Copy code The Code is as follows: <form runat = "server" id = "form1">
<Div>
<Asp: repeater id = "rptbooks" runat = "server">
<Headertemplate>
<Table id = "books" width = "90%" cellspacing = "0" style = "font-size: 12px;">
<Tr>
<TH>
Title
</Th>
<TH>
Author
</Th>
<TH>
Category
</Th>
<TH width = "30px">
</Th>
<TH width = "30px">
</Th>
</Tr>
<Tbody id = "data">
<Tr>
</Headertemplate>
<Itemtemplate>
<TD class = "title">
<% # Eval ("title") %>
</TD>
<TD class = "author">
<% # Eval ("author") %>
</TD>
<TD class = "categoryname">
<% # Eval ("categories. Name") %>
</TD>
<TD>
<A href = "#" id = '<% # eval ("ID") %> 'class = "delbook"> Delete </a>
</TD>
<TD>
<A href = 'bookdetail/<% # eval ("ID") %> 'class = "details"> details </a>
</TD>
</Itemtemplate>
<Separatortemplate>
</Tr> <tr>
</Separatortemplate>
<Footertemplate>
</Tr> </tbody>
<Tr>
<TD colspan = "3">
<PC3: pagebar id = "pagebar1" runat = "server" pagecount = "100" displaycount = "5" pageindex = "1"/>
<Input type = "text" id = "Ci"/>
</TD>
</TD>
</Tr>
</Table>
</Footertemplate>
</ASP: repeater>
</Div>
</Form>

JSON data returned by the Ajax request address: Copy code The Code is as follows: {"data": [{"title": "C #. net Program Design-foreign classical computer science textbooks "," categoryname ":". net "," author ":" Bradley (Bradley, J. c .), millspaugh,. c .) translated by Tianhong Studio "," ID ": 5392 },{" title ":" C #2.0 (baodian) (attached disk) "," categoryname ": "c ++ VC ++", "author": "Zhang Li compiled", "ID": 5027 },{ "title ": "C #2.0 complete self-taught Manual (with CD)", "categoryname": "C ++ VC ++", "author": "Edited by Zhang Li ", "ID": 5081 },{ "title": "C #2005 database programming classic tutorial", "categoryname": "C ++ VC ++ ", "author": "Karli watton, translated by Chen Qiuping", "ID": 4983 },{ "title ": "C # Programming Tutorial-Computer Basics course series", "categoryname": "C ++ VC ++", "author": "Zheng Achi, editor Liang Jingdong "," ID ": 5127 },{" title ":" C # programmer Reference Manual "," categoryname ":" C ++ VC ++ ", "author": "Grant Palmer, conbo translation", "ID": 5132 },{ "title": "C # And. net core technology-Professional Technical Series for developers "," categoryname ":" C ++ VC ++ "," author ":" (US) Perry, S. c .) translated by Xiao bin, "ID": 5104 },{ "title": "C # basic and instance tutorial (with a CD-ROM disc)", "categoryname ": "c ++ VC ++", "author": "Edited by Hao chunqiang", "ID": 5071 },{ "title ": "C # Full-process software project development analysis", "categoryname": "C ++ VC ++", "author": "(de) houm, (de) Kruse, (DE) Spada, Xue xingtao, Yuan qinyong, translated "," ID ": 5034 },{" title ":" C # design model-Master Development Series ", "categoryname": "C ++ VC ++", "author": "(US) Moco, translated by Yan", "ID": 4954}], "currentpageindex": 15, "pagecount": 1074}

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.