This page is a custom page that only reads the current data from the database. It looks like the automatic page of The GridView is basically the same.
1. Use two viewstates to save the current page number CurrentPage and total row number RowCount
# Region Paging
/// <Summary>
/// Current page
/// </Summary>
Private int CurrentPage
{
Get
{
If (ViewState ["CurrentPage"] = null)
{
Return 0; // The number of pages starts from 0.
}
Else
{
Return (INT) viewstate ["currentpage"];
}
}
Set
{
ViewState ["CurrentPage"] = value;
}
}
/// <Summary>
/// Total number of rows
/// </Summary>
Private int RowCount
{
Get
{
If (viewstate ["rowcount"] = NULL)
{
Return 0;
}
Else
{
Return (INT) viewstate ["rowcount"];
}
}
Set
{
Viewstate ["rowcount"] = value;
}
}
# Endregion
2. load data
Private void loaddata ()
{
......
Int totalcount = BLL. getrowcount (letter, teamname); // The total number of rows.
RowCount = totalCount; // Save the total number of rows to ViewState
// Obtain the data on the current page. The first two parameters are the number of records displayed on each page and the current number of records.
List <StdTeamInfo> lstStdTeam = bll. GetListByPager (GridView1.PageSize, CurrentPage + 1, letter, teamName );
GridView1.DataSource = lstStdTeam;
GridView1.DataBind ();
}
3. Add the pagination method of the GridView. The method is the same as that for automatically pagination.
Protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
This. CurrentPage = e. NewPageIndex; // Save the page to ViewState.
LoadData ();
}
4. Rewrite OnPreRender and add pagination links.
Protected override void OnPreRender (EventArgs e)
{
// Utility. GetPagerString. This method is described in step 1. The function is to obtain the paging link string.
String pagerString = Utility. GetPagerString (RowCount, (CurrentPage + 1), GridView1.PageSize, this. GridView1.ClientID );
If (pagerString! = "")
{
Int index = this. GridView1.Rows. Count;
GridViewRow rowHeader = new GridViewRow (index, index, DataControlRowType. Pager, DataControlRowState. Normal );
TableCell headerCell = new TableCell ();
HeaderCell. ColumnSpan = 8;
HeaderCell. Text = pagerString;
RowHeader. Cells. Add (headerCell );
This. GridView1.Controls [0]. Controls. Add (rowHeader );
}
}
5. GetPagerString method to obtain the pagination link string
/// <Summary>
/// Return the link string of the page
/// </Summary>
/// <Param name = "totalCount"> total number of rows </param>
/// <Param name = "currentPage"> current page (starting from 0) </param>
/// <Param name = "pageSize"> Number of records per page </param>
/// <Param name = "gridViewID"> GridView Control ID </param>
/// <Returns> </returns>
Public static string GetPagerString (int totalCount, int currentPage, int pageSize,
String gridViewID)
{
GridViewID = gridViewID. Replace ("_", "$ ");
// Total number of pages
Int pageCount = (totalCount % pageSize = 0 )? (TotalCount/pageSize): (totalCount/pageSize + 1 );
If (pagecount = 1 | pagecount = 0) Return "";
Int pagefrom = (currentpage-(currentpage % 30 = 0? 30: currentpage % 30) + 1; // set the maximum number of displayed pages to 30
If (pagefrom <0) pagefrom = 1;
Int pageto = currentpage + (30-(currentpage % 30 = 0? 30: currentpage % 30 ));
If (pageto> pagecount) pageto = pagecount;
Stringbuilder sb = new stringbuilder ();
SB. append ("<Table border = \" 0 \ "> <tr> ");
If (currentPage> 30)
Sb. append ("<td> <a href = \" javascript :__ doPostBack ('"+ gridViewID +"', 'page $ "+ (pageFrom-1) + "') \ ">... </a> </td> ");
For (int I = pageFrom; I <= pageTo; I ++)
{
If (I = currentPage)
Sb. Append ("<td> <span>" + I + "</span> </td> ");
Else
SB. append ("<TD> <a href = \" javascript :__ dopostback ('"+ gridviewid +"', 'page $ "+ I + "') \ ">" + I + "</a> </TD> ");
}
If (pageto <pagecount)
SB. append ("<TD> <a href = \" javascript :__ dopostback ('"+ gridviewid +"', 'page $ "+ (pageto + 1) + "') \ ">... </a> </TD> ");
SB. append ("<TD> total" + pagecount + "page </TD> ");
Sb. Append ("</tr> </table> ");
Return sb. ToString ();
}
Now the custom page is complete. The style is the same as the custom page of The GridView, but only the data is read, and the page display speed is much faster.