Example of querying a large amount of data in ASP. NET by PAGE

Source: Internet
Author: User

Paging is also the most frequently asked question. It is also very basic and practical.

There are a lot of paging code on the Internet. If there is no front-end, there is no back-end, there is no front-end, there is no control, or a lot of SQL code, people do not know how to use. Strive for the simplest and easiest to understand. The three-tier architecture won't be involved. Download one by yourself. If it is access, use this sqlhelper [url = http://hi.csdn.net/link.php? Url = http://blog.csdn.net/zzxap?http://hi.csdn.net/link.php? Url = http://blog.csdn.net/zzxap#/url#if it is MSSQL, use this sqlhelper [url = Alipay zzxap@hotmail.com [code = HTML] Front-end if you use the gridview, replace repeater with the gridview. Same .. <Form ID = "frm01" Action = "" method = "Post" runat = "server"> <asp: scriptmanager id = "scriptmanager1" runat = "server"> </ASP: scriptmanager> <asp: updatepanel id = "updatepanel1" runat = "server"> <contenttemplate> <Div id = "center-top"> <Div class = "Fon"> intermediate 1 </div> </div> <Div id = "center-content_Default2"> <asp: repeater runat = "server" id = "mainlist"> <itemtemplate> <Div id = "pic-out"> <li class = "pic-a"> eval ("Product_type "). tostring () </LI> <li class = "pic-B"> eval ("product_desc "). tostring () </LI> <li> price <% # eval ("price") %> </LI> </div> </itemtemplate> </ASP: repeater> </div> <Div style = "padding-top: 10px; text-align: Center"> & nbsp; Total <asp: label id = "lpagecount" forecolor = "# ff0000" runat = "server"> </ASP: Label> labels <asp: label id = "ltotalcount" forecolor = "# ff0000" runat = "server"> </ASP: Label> note <asp: linkbutton id = "fi Stpage "runat =" server "commandname =" 0 "onclick =" pager_click "> first published </ASP: linkbutton> <asp: linkbutton id = "prevpage" runat = "server" commandname = "Prev" onclick = "pager_click"> previous cursor </ASP: linkbutton> <asp: linkbutton id = "nextpage" runat = "server" commandname = "Next" onclick = "pager_click"> next cursor </ASP: linkbutton> <asp: linkbutton id = "lastpage" runat = "server" commandname = "last" onclick = "pager_click"> tail cursor </ASP: linkbutton> first <Asp: Label id = "lcurrentpage" forecolor = "# ff0000" runat = "server"> </ASP: Label> Labels & nbsp; labels to the <asp: textbox id = "gotopage" width = "30px" runat = "server" autopostback = "true" maxlength = "5"> </ASP: textbox> condition <asp: label style = "position: absolute" id = "msgbox" runat = "server" forecolor = "red" bordercolor = "red"> </ASP: label> </div> </contenttemplate> </ASP: updatepanel> </form> [/Code] [code = C #] using system; using entity E M. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. data. oledb; using system. text; using Microsoft. applicationblocks. data; public partial class _ default: system. web. UI. page {string strpage = ""; int pagecount = 0; int reccount = 0; int Cu Rrentpage = 0; int current = 0; int pages = 0; int jumppage = 0; // jump to the page where the number of records is displayed. Int pagesize = 20; // each page displays 20 records: String product_type = ""; string type = ""; string headid = ""; Private Static int pageindex = 1; Private Static int pagecounts = 1; private Static int jumppages = 1; protected void page_load (Object sender, eventargs e) {# region receives parameters passed from the query page. If (request. querystring ["product_type"]! = NULL) {product_type = server. urldecode (request. querystring ["product_type"]. tostring () ;}if (request. querystring ["type"]! = NULL) {type = server. urldecode (request. querystring ["type"]. tostring () ;}# endregion // headid = request. querystring ["headid"]; If (! Ispostback) {// ------------------------------------------------------------- reccount = calc (); // calculate the total number of records pagecount = reccount/pagesize + overpage (); // calculate the total number of pages pagecounts = reccount/pagesize-modpage (); If (! String. isnullorempty (strpage) {// set the current page to return pageindex = int. parse (strpage);} else {pageindex = 1; // set the current page to 11 // session ["curpage"] = 1;} jumppages = pagecount; lpagecount. TEXT = pagecount. tostring (); // the total number of pages ltotalcount. TEXT = reccount. tostring (); // The total number of records if (reccount <= pagesize) {gotopage. enabled = false;} else {gotopage. enabled = true ;}//------------------------------------------------------- ------ Getmaindata (); // bind data }}# region "calculates the total number of rows" Public int overpage () {// calculates the remainder int pages = 0; if (reccount % pagesize! = 0) {pages = 1;} else {pages = 0;} return pages;} public int modpage () {// calculate the remainder int pages = 0; if (reccount % pagesize = 0 & reccount! = 0) {pages = 1;} else {pages = 0;} return pages;} public int calc () {// calculate the total data dataset DS = new dataset (); stringbuilder ass = new stringbuilder ("select count (ID) from m_product where 1 = 1 "); // If a query parameter is input, the total number of query results is calculated. If (type! = NULL & type! = "") {Ass. append ("and [type] =" + type. Replace ("'", "") + "");} If (product_type! = NULL & product_type! = "") {Ass. append ("and product_type =" + product_type.replace ("'", "") + "");} string BSS = ass. tostring (); int recordcount = 0; DS = sqlhelper. executedataset (sqlhelper. conn, commandtype. text, BSS); recordcount = int32.parse (Ds. tables [0]. rows [0] [0]. tostring (); // recordcount = 10; If (recordcount <pagesize) {fistpage. enabled = false; prevpage. enabled = false; nextpage. enabled = false; lastpage. Enabled = false;} else {fistpage. enabled = true; prevpage. enabled = true; nextpage. enabled = true; lastpage. enabled = true;} return recordcount;} # endregion # region "flip" Public void pager_click (Object sender, eventargs e) {currentpage = (INT) pageindex; pages = (INT) pagecounts; string Arg = (linkbutton) sender ). commandname. tostring (); Switch (ARG) {Case "Next": // URL paging is supported for the next page. If (currentpage <pages + 1) {currentpage = currentpage + 1;} break; Case "Prev": // previous page if (currentpage! = 1) {currentpage-= 1;} break; Case "last": // The last page currentpage = pages + 1; break; default: // homepage currentpage = 1; break;} // control the availability of the page flip button based on the number of pages. If (currentpage> 1) {fistpage. enabled = true; prevpage. enabled = true;} else {fistpage. enabled = false; prevpage. enabled = false;} If (currentpage = pages + 1) {nextpage. enabled = false; lastpage. enabled = false;} else {nextpage. enabled = true; Las Tpage. enabled = true;} pageindex = currentpage; // obtain the changed page number. // session ["curpage"] = currentpage; // The user returns to the current page getmaindata ();} // the page to which protected void gotopage_textchanged (Object sender, system. eventargs e) {string ASD = This. gotopage. text. trim (). tostring (); jumppage = (INT) jumppages; If (string. isnullorempty (ASD) {alert ("out of page range"); return;} If (int32.parse (gotopage. text)> jumppage | int 32. parse (gotopage. text) <= 0 | string. isnullorempty (ASD) {alert ("out of page range"); // The number of pages exceeds the return;} else {int inputpage = int32.parse (gotopage. text. tostring (); pageindex = inputpage; // session ["curpage"] = inputpage; getmaindata (); // bind the dataset }}# endregion public void alert (string RTT) {// display the prompt information // system. web. UI. scriptmanager. registerclientscriptblock (updatepanel1, this. getType (), "ajaxmsgbo X "," alert ('"+ RTT +"'); ", true);} private void getmaindata () {currentpage = (INT) pageindex; // obtain the current page pages = (INT) pagecounts; // obtain the total number of pages lcurrentpage. TEXT = currentpage. tostring (); int pagesize2 = pagesize * (currentpage-1) + 1; if (type! = NULL & product_type! = NULL) // if the phone number {// The number of function parameters is incorrect in the query expression 'isnull (max (XX. ID), 0. // The select clause contains a reserved word, spelling error or missing parameter, or incorrect punctuation. // The core statement of paging determines the performance of row_number. You can also change the following statement to a stored procedure. Stringbuilder SQL = new stringbuilder ("select top" + pagesize + "* From m_product a"); SQL. append ("where. [ID]> (select max (XX. [ID]) from (select top "+ pagesize2 +" [ID] From m_product where 1 = 1 "); If (type! = NULL & type! = "") {SQL. append ("and [type] =" + type. Replace ("'", "") + "");} If (product_type! = NULL & product_type! = "") {SQL. append ("and product_type =" + product_type.replace ("'", "") + "");} SQL. append ("order by [ID]) xx)"); If (type! = NULL & type! = "") {SQL. append ("and A. [type] =" + type. Replace ("'", "") + "");} If (product_type! = NULL & product_type! = "") {SQL. append ("and. product_type = "+ product_type.replace (" '"," ") +" ");} SQL. append ("order by. [ID] "); mainlist. datasource = sqlhelper. executedataset (sqlhelper. conn, commandtype. text, SQL. tostring (); mainlist. databind () ;}} [/Code]

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.