Example code for the. NET GridView page Template _ Practical Tips

Source: Internet
Author: User

Copy Code code as follows:

To add the following attributes to the GridView (foreground or backstage look at your habits.) )

Implementing pagination

Allowpaging= "true"

One page of data 10 rows

Pagesize= "10"

Events that are triggered when paging
Onpageindexchanging= "Gvwdesignationname_pageindexchanging"

In server events

protected void Gvwdesignationname_pageindexchanging (object sender, Gridviewpageeventargs e)
{
Gvwdesignationname.pageindex=e.newindex;

Bingdesignatioonname ();
}


Here I give a generic display pagination template

<PagerTemplate>
Current article:
(GridView) Container.namingcontainer) is to get the current control
<asp:label id= "Labelcurrentpage" runat= "Server" text= "<%# ((GridView) container.namingcontainer). PageIndex + 1%> "></asp:Label>
Page/Total:
Get the total number of paging pages
<asp:label id= "Labelpagecount" runat= "Server" text= "<%# ((GridView) container.namingcontainer). PageCount%> "></asp:Label>
Page
If the page is the first page, then the connection will not be displayed. At the same time, the command parameter commandargument with self-identification is corresponding.
<asp:linkbutton id= "Linkbuttonfirstpage" runat= "Server" commandargument= "a", "Page"
Visible= ' <%# (GridView) container.namingcontainer). PageIndex!= 0%> ' > Home </asp:LinkButton>
<asp:linkbutton id= "Linkbuttonpreviouspage" runat= "Server" commandargument= "Prev"
Commandname= "Page" visible= ' <%# (GridView) container.namingcontainer). PageIndex!= 0%> ' > prev </asp:LinkButton>
If the paging is the last, then the connection will not display
<asp:linkbutton id= "Linkbuttonnextpage" runat= "Server" commandargument= "Next" commandname= "Page"
Visible= ' <%# (GridView) container.namingcontainer). PageIndex!= ((GridView) container.namingcontainer). PageCount-1%> ' > next page </asp:LinkButton>
<asp:linkbutton id= "Linkbuttonlastpage" runat= "Server" commandargument= "last" commandname= "Page"
Visible= ' <%# (GridView) container.namingcontainer). PageIndex!= ((GridView) container.namingcontainer). PageCount-1%> ' > Last </asp:LinkButton>
Go to Page
<asp:textbox id= "Txtnewpageindex" runat= "Server" width= "20px" text= ' <%# (GridView) Container.Parent.Parent). PageIndex + 1%> '/> page
Here will commandargument even click the button E.newindex value is 3
<asp:linkbutton id= "Btngo" runat= "Server" causesvalidation= "False" commandargument= "-2"
Commandname= "Page" text= "Go"/>
</PagerTemplate>

The code for 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 the Go button
TextBox txtnewpageindex = null;

The GridView provides more APIs than the DataGrid, acquiring paging blocks that can use Bottompagerrow or Toppagerrow, and of course adding headerrow and Footerrow
GridViewRow pagerrow = Thegrid.bottompagerrow;

if (Pagerrow!= null)
{
Get the text control
Txtnewpageindex = Pagerrow.findcontrol ("Txtnewpageindex") as TextBox;
}
if (txtnewpageindex!= null)
{
Get indexed
NewPageIndex = Int. Parse (Txtnewpageindex.text)-1;
}
}
Else
{
Click on 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;

Re-bind
Bingdesignatioonname ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.