Resolve the built-in pagination of the GridView and use it with DropDownList. the built-in pagination of the gridview

Source: Internet
Author: User

Resolve the built-in pagination of the GridView and use it with DropDownList. the built-in pagination of the gridview

Implement the pagination function provided by the GridView:

To implement the GrdView paging Function

The procedure is as follows:

  1. Change the AllowPaging attribute of the GrdView control to true.
  2. Change the PageSize attribute of the GrdView control to any value (10 by default)
  3. Change PageSetting-> Mode of the GrdView control to Numeric (default: Numeric). This attribute is a paging style.

After the GridView property is set, the page style is displayed on the page.

Now we start to implement the paging function:

  1. After <asp: GridView ID =...>, add OnPageIndexChanging = "GridView1_PageIndexChanging"
  2. In the corresponding aspx. cs, add:
Protected void gridviewinclupageindexchanging (object sender, GridViewPageEventArgs e) {GridView1.PageIndex = e. NewPageIndex; InitPage (); // rebind the function of the GridView data}

Reference code:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "gridview_zidaifenye.aspx.cs" Inherits = "gridview_zidaifenye" %> <! DOCTYPE html> 

Background code:

Using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; public partial class gridview_zidaifenye: System. web. UI. page {DBAccess db = new DBAccess (); protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {// gvProduct. dataSource = getData (); // gvProduct. dataBind (); BindGrid () ;}} public void BindGrid () {SqlCommand comm = db. createCommand ("select * from product p, Uuser u where p. userid = u. id "); SqlDataAdapter sda = new SqlDataAdapter (); sda. selectCommand = comm; DataSet ds = new DataSet (); sda. fill (ds, "Datatable"); DataView dv = ds. tables [0]. defaultView; GridView1.DataSource = dv; GridView1.DataBind ();} protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e) {GridView1.PageSize = int. parse (messages); GridView1.PageIndex = 0; BindGrid (); // GridView1.DataBind ();} protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e) {lblMsg. text = "the current page is" + (GridView1.PageIndex + 1 ). toString () + "Page, total" + (GridView1.PageCount ). toString () + "page";} protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e) {GridView1.PageIndex = (GridViewPageEventArgs) e ). newPageIndex; BindGrid (); // rebind the function of the GridView data }}

In conclusion, it is convenient for future use.

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.