The implementation idea is the same as the Excel and Word export above, which is to add rows in the GridView. First, declare the following controls to display the page number: which page, how many pages in total, how many records, home page, previous page, next One page, last page controls for paging
Label lblCurrentPage;
Label lblPageCount;
Label lblRowsCount;
LinkButton btnFirst;
LinkButton btnPrev;
LinkButton btnNext;
LinkButton btnLast; In the OnInit method of GridView, initialize these controls. Initialize the paging control in the Oninit method of the control.
protected override void OnInit(EventArgs e)
{
this.EnableViewState = true;
lblCurrentPage = new Label();
lblCurrentPage.ForeColor = ColorTranslator.FromHtml("#e78a29");
lblCurrentPage.Text = "1";
lblPageCount = new Label();
lblPageCount.Text = "1";
lblRowsCount = new Label();
lblRowsCount.ForeColor = ColorTranslator.FromHtml("#e78a29");
btnFirst = new LinkButton();
btnFirst.Text = "Homepage";
btnFirst.Command += new CommandEventHandler(NavigateToPage);
btnFirst.CommandName = "Pager";
btnFirst.CommandArgument = "First";
btnPrev = new LinkButton();
btnPrev.Text = "Previous page";
btnPrev.Command += new CommandEventHandler(NavigateToPage);
btnPrev.CommandName = "Pager";
btnPrev.CommandArgument = "Prev";
btnNext = new LinkButton();
btnNext.Text = "Next page";
btnNext.Command += new CommandEventHandler(NavigateToPage);
btnNext.CommandName = "Pager";
btnNext.CommandArgument = "Next";
btnLast = new LinkButton();
btnLast.Text = "Last page";
btnLast.Command += new CommandEventHandler(NavigateToPage);
btnLast.CommandName = "Pager";
btnLast.CommandArgument = "Last";
base.OnInit(e);
}
Then the key part of the code is how to add these controls to the GridView by creating sub-controls, as follows:
Add paging controls in the method of creating child controls
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
int res = base.CreateChildControls(dataSource, dataBinding);
if(ShowToolBar)
{
try
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Pager, DataControlRowState.Normal);
TableCell c = new TableCell();
c.Width = Unit.Percentage(100);
c.ColumnSpan = this.Columns.Count;
row.Cells.Add(c);
TableCell cell1 = new TableCell();
Table table = new Table();
TableRow r = new TableRow();
table.Rows.Add(r);
table.Width = Unit.Percentage(100);
c.Controls.Add(table);
r.Cells.Add(cell1);
Literal l1 = new Literal();
l1.Text = "Page:";
cell1.Controls.Add(l1);
cell1.Wrap = false;
cell1.Controls.Add(lblCurrentPage);
l1 = new Literal();
l1.Text = "Page/";
cell1.Controls.Add(l1);
cell1.Controls.Add(lblPageCount);
l1 = new Literal();
l1.Text = "Page, total";
cell1.Controls.Add(l1);
cell1.Controls.Add(lblRowsCount);
l1 = new Literal();
l1.Text = "record";
cell1.HorizontalAlign = HorizontalAlign.Left;
cell1.Controls.Add(l1);
TableCell cell2 = new TableCell();
cell2.HorizontalAlign = HorizontalAlign.Right;
cell2.Wrap = false;
l1 = new Literal();
l1.Text = "[";
cell2.Controls.Add(l1);
cell2.Controls.Add(btnFirst);
l1 = new Literal();
l1.Text = "] ";
cell2.Controls.Add(l1);
l1 = new Literal();
l1.Text = "[";
cell2.Controls.Add(l1);
cell2.Controls.Add(btnPrev);
l1 = new Literal();
l1.Text = "] ";
cell2.Controls.Add(l1);
l1 = new Literal();
l1.Text = "[";
cell2.Controls.Add(l1);
cell2.Controls.Add(btnNext);
l1 = new Literal();
l1.Text = "] ";
cell2.Controls.Add(l1);
l1 = new Literal();
l1.Text = "[";
cell2.Controls.Add(l1);
cell2.Controls.Add(btnLast);
l1 = new Literal();
l1.Text = "] ";
cell2.Controls.Add(l1);
r.Cells.Add(cell2);
this.Controls[0].Controls.AddAt(0, row);
}
catch
{
}
}
return res;
}The following is the event that handles paging, similar to RowCommand
public void NavigateToPage(object sender, CommandEventArgs e)
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = true;
btnLast.Enabled = true;
switch (e.CommandArgument.ToString())
{
case "Prev":
if (this.PageIndex > 0)
{
this.PageIndex -= 1;
}
break;
case "Next":
if (this.PageIndex < (this.PageCount - 1))
{
this.PageIndex += 1;
}
break;
case "First":
this.PageIndex = 0;
break;
case "Last":
this.PageIndex = this.PageCount - 1;
break;
}
if (this.PageIndex == 0)
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
if (this.PageCount == 1)
{
btnLast.Enabled = false;
btnNext.Enabled = false;
}
}
else if (this.PageIndex == this.PageCount - 1)
{
btnLast.Enabled = false;
btnNext.Enabled = false;
}
OnBind();
}In this way, a personalized paging can be easily realized. Welcome to join us.