Asp. NET Simple SQL Paging implementation

Source: Internet
Author: User

Today is out of the 32nd day of internship, in school like swallowed, what is so-so, and paging such a need for logic like me lazy people like to use plug-ins, carefully fathom the next is not good, so it took a little time to study the next pagination,

For today, it's a simple SQL statement. Implementation of pagination in ASP

How do I write SQL statements?

Because you want to write a simple SQL statement to implement paging, SQL is naturally not very difficult!

1.IN less in writing efficiency

--in and not in, less efficient --5 ID from TableName)

2. By ascending and descending order

-- pagination by sort --* from    (5 * from          TableName ORDER BY ID ASC) as             TEMP1 order by ID DESC) as        TEMP2 ORDER by ID ASC

3. Using the SQL Server Row Num () function

-- first, the Row_number () function is used with the over package -- first, through the Row_number () function to query the first 10 data and then all and more than 0 data -- Simply speaking Top10 Corresponds to how many data is displayed on a page and 0 corresponds to the current page select from (selectten as from Sqlpagetest)aswhere xxx>0

Implementing paging

DBHelper

 public  DataTable pagebysql (int   PageSize, int   PageIndex) { string  sql = string . Format ( " select * FROM (select top {0} row_number () Over (order by ID) as xxx,* from sqlpagetest) as temp where xxx>{1}   " , pageSize, PageIndex);            DataTable tb  = new   DataTable (); SqlDataAdapter dp  = new   SqlDataAdapter (sql            , conn); Dp.            Fill (TB);         return   TB; }

Front Code

<form id="Form1"runat="Server"> <div> <table> <tr> <td>ID</td> <td> name </td> </tr> <asp:repeater id="Repeater1"runat="Server"> <ItemTemplate> <tr> <td><%# Eval ("ID")%></td> <td><% #Eval ("name")%></td> </tr> </ItemTemplate> </asp:rep Eater> </table> <%--show pagesize--%> <asp:hiddenfield id="HiddenField1"runat="Server"/> <%--display pageindex--%> <asp:hiddenfield id="HiddenField2"runat="Server"/> <asp:linkbutton id="LinkButton1"onclick="LinkButton1_Click"runat="Server"> Prev </asp:LinkButton> <asp:linkbutton id="LinkButton2"onclick="Linkbutton2_click"runat="Server"> Next </asp:LinkButton> </div> </form>

Background code

 Public intMaxpage;  Public intNowpage; protected voidPage_Load (Objectsender, EventArgs e) {            if(!IsPostBack) {Maxpage=Ten; Nowpage=0;                Page (Maxpage, nowpage); Hiddenfield1.value=Maxpage.                ToString (); Hiddenfield2.value=Nowpage.            ToString (); }        }         Public voidPageintMaxpageintnowpage) {Repeater1.datasource=NewDBHelper ().            Pagebysql (Maxpage, nowpage);        Repeater1.databind (); }        //Previous Page        protected voidLinkButton1_Click (Objectsender, EventArgs e) {            if(int. Parse (Hiddenfield2.value) >0) {Maxpage=int. Parse (Hiddenfield1.value)-Ten; Nowpage=int. Parse (Hiddenfield2.value)-Ten;                Page (Maxpage, nowpage); Hiddenfield1.value=Maxpage.                ToString (); Hiddenfield2.value=Nowpage.            ToString (); }            Else            {            }        }        //Next Page        protected voidLinkbutton2_click (Objectsender, EventArgs e) {Maxpage+=(Ten+int.                Parse (Hiddenfield1.value)); Nowpage+= (Ten+int.                Parse (Hiddenfield2.value));                Page (Maxpage, nowpage); Hiddenfield1.value=Maxpage.                ToString (); Hiddenfield2.value=Nowpage.                   ToString (); }

The Earth is the mountain, and the water is the sea. --Xun Zi

Asp. NET Simple SQL Paging implementation

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.