Implementation of GridView paging and instances of custom page style functionality
- 2020-06-19 10:04:27
- OfStack
Implementation of GridView paging
In the server event
Here I present a common template for displaying pagination.
The code in the event should be
Want to be in GridView add
// Implement paging
AllowPaging="true"
//1 Page data 10 line
PageSize="10"
// Events that are triggered when paging
OnPageIndexChanging="gvwDesignationName_PageIndexChanging"
In the server event
protectedvoid gvwDesignationName_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvwDesignationName.PageIndex=e.newIndex;
bingDesignatioonName();
}
Here I present a common template for displaying pagination.
<PagerTemplate>
The current first :
//((GridView)Container.NamingContainer) To get the current control
<asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
page / A total of :
// Gets the total number of paginated pages
<asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
page
// If the page is the first page, the connection is not displayed . At the same time corresponding to the built-in identification of the command parameters CommandArgument
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'> Home page </asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'> on 1 page </asp:LinkButton>
// If the page is the last page, the connection is not displayed
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'> Under the 1 page </asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'> back </asp:LinkButton>
Turn to the first
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' /> page
// There will be CommandArgument Even if you click the button e.newIndex A value of 3
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2"
CommandName="Page" Text="GO" />
</PagerTemplate>
The code in the event should be
protected void gvwDesignationName_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Get the control
GridView theGrid = sender as GridView;
int newPageIndex = 0;
if (e.NewPageIndex==-3)
{
// Click on the Go button
TextBox txtNewPageIndex = null;
//GridView a DataGrid More API , which can be used to get the paging block BottomPagerRow or TopPagerRow Of course, it has increased HeaderRow and FooterRow
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (pagerRow != null)
{
// get text controls
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
}
if ( txtNewPageIndex!= null)
{
// Get the index
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
}
}
else
{
// Click the other button
newPageIndex = e.NewPageIndex;
}
// Prevent new index overflow
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
// Get a new value
theGrid.PageIndex = newPageIndex;
// rebind
bingDesignatioonName();
}