The implementation code is as follows:
Copy Code code as follows:
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Drawing;
Namespace ShowData4
{
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Gridview1.pagesize = 5; The number of records that the/*gridview control displays on each page * *
if (GridView1.Rows.Count!= 0)/* Load a tab when the number of records is displayed only one page * *
{
Control table = Gridview1.controls[0];
int count = table. Controls.Count;
Table. CONTROLS[COUNT-1]. Visible = true;
}
}
protected void gridview1_rowcreated (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.pager)/* Display the line of the pager control * *
{
/* Create a button to display a hyperlink on a Web page * *
LinkButton Button_indexfirst = new LinkButton ();
LinkButton button_indexlast = new LinkButton ();
LinkButton button_indexnext = new LinkButton ();
LinkButton button_indexprevious = new LinkButton ();
/* Add hyperlink button to pager line * *
E.row.controls[0]. Controls.Add (Button_indexfirst);
E.row.controls[0]. Controls.Add (New LiteralControl) (("")); /* The paging buttons are separated by 2 spaces */
E.row.controls[0]. Controls.Add (button_indexprevious);
E.row.controls[0]. Controls.Add (New LiteralControl) ((""));
E.row.controls[0]. Controls.Add (Button_indexnext);
E.row.controls[0]. Controls.Add (New LiteralControl) ((""));
E.row.controls[0]. Controls.Add (Button_indexlast);
Button_indexfirst.text = "First page";
Button_indexfirst.commandname = "a";
Button_indexfirst.click + = new EventHandler (Pagebuttonclick);
Button_indexprevious.text = "Previous page";
Button_indexprevious.commandname = "Previous";
Button_indexprevious.click + = new EventHandler (Pagebuttonclick);
Button_indexnext.text = "Next page";
Button_indexnext.commandname = "Next";
Button_indexnext.click + = new EventHandler (Pagebuttonclick);
Button_indexlast.text = "Last page";
Button_indexlast.commandname = "Last";
Button_indexlast.click + = new EventHandler (Pagebuttonclick);
if (gridview1.pageindex = 0)
{
if (Gridview1.pagecount > 1)/* The number of pages required is greater than one page * *
{
button_indexfirst.enabled = false;
button_indexprevious.enabled = false;
}
else/* Record count is only one page * *
{
button_indexfirst.enabled = false;
button_indexprevious.enabled = false;
button_indexnext.enabled = false;
button_indexlast.enabled = false;
}
}
else if (Gridview1.pageindex = = gridview1.pagecount-1)
{
button_indexnext.enabled = false;
button_indexlast.enabled = false;
}
else if (gridview1.pagecount <= 0)
{
Response.Write (There is no data in the datasheet!) ");
button_indexfirst.enabled = false;
button_indexprevious.enabled = false;
button_indexnext.enabled = false;
button_indexlast.enabled = false;
}
}
}
protected void Pagebuttonclick (object sender, EventArgs e)
{
LinkButton Clickedbutton = ((LinkButton) sender);
if (Clickedbutton.commandname = = "First")/* Click on the "Page One" button, the page index is 0*/
{
Gridview1.pageindex = 0;
}
else if (Clickedbutton.commandname = "Next")/* Click on "Next" button, page index plus 1*/
{
if (Gridview1.pageindex < gridview1.pagecount-1)
{
Gridview1.pageindex + 1;
}
}
else if (Clickedbutton.commandname = = "previous")/* Click on the "Prev" button, page index if greater than or equal to 1 minus 1*/
{
if (Gridview1.pageindex >= 1)
{
Gridview1.pageindex-= 1;
}
}
else if (Clickedbutton.commandname = "Last") * * Click on the "final page" button * *
{
Gridview1.pageindex = gridview1.pagecount-1;
}
}
}
}