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:
- Change the AllowPaging attribute of the GrdView control to true.
- Change the PageSize attribute of the GrdView control to any value (10 by default)
- 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:
- After <asp: GridView ID =...>, add OnPageIndexChanging = "GridView1_PageIndexChanging"
- 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!