Example: ASP.net in the GridView implementation of paging function

Source: Internet
Author: User
Tags numeric table name

To implement the Grdview paging functionality

The operation is as follows:

1, change the AllowPaging property of the Grdview control to True.
2. Change the PageSize property of the Grdview control to any value (default is 10)
3, change the Grdview control Pagesetting->mode to numeric, etc. (default is numeric) This property is a paging style.
The GridView property is set up so that you can see the paging style from the page as well.

Now it's time to implement the paging feature:

1, after <<asp:gridview id=......> Add, onpageindexchanging= "gridview1_pageindexchanging"
2, in the corresponding aspx.cs add:
protected void Gridview1_pageindexchanging (object sender, Gridviewpageeventargs e)
{
Gridview1.pageindex = E.newpageindex;
Initpage (); Functions that rebind the GridView data
}

Example:

Features: GridView pagination uses the picture button and adds a page numbering display.

By default, the GridView Paging button cannot display text if it is displayed as a picture, so you cannot know the number of pages currently in the page. You can then add the paging code display to display the index number of the page you are paging to.

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;

public partial class GridView_Page:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Sets the picture buttons for pagination, which can be set in pagersetting on the control's property sheet
if (! IsPostBack)
{
Gridview1.caption = "This is a small experiment of the GridView";
The Caption property is similar to the table name and is displayed directly above the control.
GridView1.PagerSettings.Mode = Pagerbuttons.nextpreviousfirstlast;
GRIDVIEW1.PAGERSETTINGS.NEXTPAGEIMAGEURL = "Img/next.gif";
GRIDVIEW1.PAGERSETTINGS.PREVIOUSPAGEIMAGEURL = "Img/pre.gif";
GRIDVIEW1.PAGERSETTINGS.FIRSTPAGEIMAGEURL = "Img/first.gif";
GRIDVIEW1.PAGERSETTINGS.LASTPAGEIMAGEURL = "Img/last.gif";
Gridview1.pagesize = 10; Display up to 10 records per page;
Binddata ();
}
}
private void Binddata ()
{
Deploy data to the GridView
String constr = "Server=localhost;" Uid=sa;pwd=123456;database=northwind ";
String sqlstr = "SELECT * FROM Products";
SqlConnection con = new SqlConnection (CONSTR);
SqlDataAdapter ad = New SqlDataAdapter (Sqlstr, con);
DataSet ds = new DataSet ();
Ad. Fill (DS);
Gridview1.datasource = ds;
Gridview1.databind ();
}
protected void Gridview1_pageindexchanged (object sender, EventArgs e)
{
After paging, redeploy the data
Binddata ();
}
protected void Gridview1_pageindexchanging (object sender, Gridviewpageeventargs e)
{
Before paging is complete
Gridview1.pageindex = E.newpageindex;
}
protected void Gridview1_databound (object sender, EventArgs e)
{
Add page numbering display
GridViewRow bottompagerrow = Gridview1.bottompagerrow;
Label Bottomlabel = new label ();
Bottomlabel.text = "Currently in the paging: (" + (Gridview1.pageindex + 1) + "/" + Gridview1.pagecount + ")";
Bottompagerrow.cells[0]. Controls.Add (Bottomlabel);
}
}

Related Article

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.