Use the gridview by PAGE

Source: Internet
Author: User

The gridview control comes with pagination. After the data source control is bound, allowpaging can automatically implement pagination. This is the code implementation paging method.

 

In the pageload event, specify the number of pages allowed and the number of pages per page.

GridView2.AllowPaging = true;GridView2.PageSize = 5;

When you need to use your own page flip interface layout, the gridview provides the editing template. Select pagertemplate In the Template Selection drop-down box to customize the Layout

<Asp: gridview id = "gridview2" runat = "server" autogeneratecolumns = "false" onpageindexchanging = "inline" allowpaging = "true" pagesize = "2" pageindex = "1"> <columns> <asp: boundfield datafield = "ID" headertext = "ID"/> <asp: boundfield datafield = "name" headertext = "name"/> <asp: boundfield datafield = "code" headertext = "encoding"/> <asp: boundfield datafield = "level" headertext = "level"/> </columns> <PAG Ertemplate> current No.: <% -- (gridview) container. namingcontainer) is used to obtain the current gridview control -- %> <asp: Label id = "labelcurrentpage" runat = "server" text = "<% # (gridview) container. namingcontainer ). pageindex + 1%> "> </ASP: Label> page/Total: <asp: Label id =" labelpagecount "runat =" server "text =" <% # (gridview) container. namingcontainer ). pagecount %> "> </ASP: Label> page <% -- the homepage button is not displayed when the page is the first page; the previous page and the next page are the last page, the button on the last page does not display commandargument. Used to set the commandname attribute. It is used as a parameter to add the commandname attribute and provides an optional parameter for the command event handler to be executed. For example, set the commandname attribute to sort and set the commandargument attribute to ascending to specify commands in ascending order. -- %> <% -- Home page button, corresponding to the Command Parameter commandargument -- %> <asp: linkbutton id = "linkbuttonfirstpage" runat = "server" commandargument = "first" commandname = "page" visible = '<% # gridview2.pageindex! = 0%> '> homepage </ASP: linkbutton> <asp: linkbutton id = "linkbuttonpreviouspage" runat = "server" commandargument = "Prev" commandname = "page" visible = '<% # (gridview) container. namingcontainer ). pageindex! = 0%> '> previous page </ASP: linkbutton> <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 page </ASP: linkbutton> to the <asp: textbox id = "txtnewpageindex" runat = "server" width = "20px"/> page <% -- commandargument is displayed here even if you click this button E. the newindex value is 3 -- %> <asp: linkbutton id = "btngo" runat = "server" causesvalidation = "false" commandargument = "-1" commandname = "page" text = "go"/> </pagertemplate> </ ASP: gridview>
View code

Code

(GridView)Container.NamingContainer

 

 

It is used to obtain the gridview in the pagertemplate of the gridview and display and hide the buttons judged by the pageindex attribute.

 

Background operations only need to be processed in the pageindexchanging event.

   private void Bind(string sqlStr)    {          SqlData SqlData = new SqlData();        DataTable dt = SqlData.GetDataTableFromDB(sqlStr);        GridView2.DataSource = dt;        GridView2.DataBind();    }    

The sqldata class is a database connection class. Obtain a data table.

 

Protected void gridview2_pageindexchanging (Object sender, gridviewpageeventargs e) {int newpageindex = 0; If (E. newpageindex <0) {// click the go button textbox txtnewpageindex = NULL; // The gridview provides more APIs than the DataGrid. You can use bottompagerrow or toppagerrow to obtain the paging block, of course, headerrow and footerrow gridviewrow pagerrow = gridview2.bottompagerrow; If (pagerrow! = NULL) {// obtain the text control txtnewpageindex = pagerrow. findcontrol ("txtnewpageindex") as textbox;} If (txtnewpageindex! = NULL) {// obtain the index newpageindex = int. parse (txtnewpageindex. text)-1 ;}} else {// click another button newpageindex = E. newpageindex;} // prevents the new index from overflowing with newpageindex = newpageindex <0? 0: newpageindex; newpageindex = newpageindex> = gridview2.pagecount? Gridview2.pagecount-1: newpageindex; // obtain the new value gridview2.pageindex = newpageindex; // rebind string sqlstr = "select [ID], [name], [Code], [level] from [addr_officialcity] "; this. BIND (sqlstr );}
View code

 

 

The page is displayed, Which is concise and clear.

 

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.