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;
}
}