Relearn & amp; gt; ASP. NET small instance & amp; gt; DataReader manual Paging

Source: Internet
Author: User

It took two days to write it based on others,

First, the table data is as follows:


 

Directly run the Code:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data;
Using System. Data. SqlClient;
Using System. Configuration;

Public partial class learnFenye: System. Web. UI. Page
{
SqlConnection sqlconn;
Protected void Page_Load (object sender, EventArgs e)
{
Int PageSize = 3; // number of entries per page
Int PageCount = 0; // total number of pages
Int RecordCount = 0; // number of database records
Int CurrentPage = 0; // current page number


String conn = ConfigurationManager. ConnectionStrings ["conn"]. ConnectionString;
Sqlconn = new SqlConnection (conn );
Sqlconn. Open ();
// Step 1: calculate the number of records in the database
// RecordCount =
RecordCount = CountRecord ();
Response. Write ("number of database records:" + RecordCount + "<br/> ");

// Step 2: Calculate the page number
PageCount = RecordCount/PageSize;
If (RecordCount % PageSize> 0) // if the entire page cannot be deleted, an additional page is required.
{
PageCount = PageCount + 1;
}
Response. Write ("Page pages:" + PageCount + "<br/> ");


// Step 3: Obtain the current page number
If (Request. QueryString ["page"]! = Null)
{
CurrentPage = Convert. ToInt32 (Request. QueryString ["page"]);
}
Else
{
CurrentPage = 1; // if not set, it starts from the first page.
}
Response. Write ("the current page number is:" + CurrentPage + "<br/> ");


// Step 4: start paging
String SQL = "SELECT TOP (" + PageSize + ") TicketAutoId, value FROM Ticket ";
SQL + = "where TicketAutoId not in (select top" + (CurrentPage-1) * PageSize + "TicketAutoId from Ticket order by TicketAutoId )";
SQL + = "ORDER BY TicketAutoId ";
SqlCommand sqlcmd = new SqlCommand (SQL, sqlconn );
SqlDataReader sqldr = sqlcmd. ExecuteReader ();
String HTMLTable;
HTMLTable = "<table> ";
While (sqldr. Read ())
{

HTMLTable + = "<tr> <td>" + sqldr. GetInt32 (0). ToString () + "</td> </tr> ";
// Response. Write (sqldr. GetInt32 (0 ));
}
HTMLTable + = "</table> ";
Response. Write (HTMLTable );
// Sqldr. Close ();

// Step 5: Set the homepage, Previous Page, next page, and last page.
Int pagePre, pageNext;
PagePre = CurrentPage-1;
PageNext = CurrentPage + 1;
String pageHtml;

If (pagePre = 0)
{
PageHtml = "homepage & nbsp; Previous Page ";
}
Else
{
PageHtml = "<a href = learnfenye. aspx? Page = 1> homepage </a> & nbsp; <a href = learnfenye. aspx? Page = "+ pagePre +"> previous page </a> ";
}

If (pageNext> PageCount)
{
PageHtml + = "& nbsp; next & nbsp; last page ";
}
Else
{
PageHtml + = "& nbsp; <a href = learnfenye. aspx? Page = "+ pageNext +"> next page </a> ";
PageHtml + = "& nbsp; <a href = learnfenye. aspx? Page = "+ PageCount +"> last page </a> ";
}
IndexButton. InnerHtml = pageHtml;

}

Private int CountRecord () // count the number of records in the database table
{
Int count = 0;
String SQL = "select count (*) from Ticket ";
SqlCommand sqlcmd = new SqlCommand (SQL, sqlconn );
SqlDataReader sqldr = sqlcmd. ExecuteReader ();
If (sqldr. Read ()! = Null)
{
Count = sqldr. GetInt32 (0 );
}
Sqldr. Close ();
Return count;
}
}

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.