asp.net pagination and Aspnetpager controls use _ Practical tips

Source: Internet
Author: User
Here's a few ways to focus on these pages:

In general, we are paging through SQL statements, which are common in any development of speech,

The most important way to use SQL statements or stored procedure paging is to write the SQL statement or stored procedure when reading the data, which is to read only the few rows currently being displayed, so write the statements based on the number of pages and each page displayed, as follows:

Select Top PageSize * from news where ID. (select top (Page-1) *pagesize ID from News)

Obviously, just use pagesize and page as arguments, then when the front page calls to specify the current page and the number of display per page, you can implement pagination, in order to display the total number, you can also read all the number, you need to pay attention to, if it is a custom paging view, Because. NET does not save the current page when it comes back, use ViewState to save pages, read only a few records at a time, so it is suitable to do large number of websites when using

Another way to pagination is to use the Pagedatasource class provided by. NET to page through.

Pagedatasource is Microsoft to provide a class for paging use, the integration of some of the methods of binding controls, in use, we only need to read from the database we want the data, binding to Pagedatasource, Then allow Pagedatasource to page, specify the current page and the number of pages to display, so that you can get a good total number, combined with the control, viewstate can complete the custom paging view, as follows:

PagedDataSource Pagedatasource = new PagedDataSource ();

 < /c12>   < /c33>  < /c54> pagedatasource.datasource = Newsmanager.getnews ();//All Records

 < /c66>  < /c87>  < /c107> pagedatasource.allowpaging = true;

 < /c12>   < /c33>  < /c54> < c76>pagedatasource.pagesize= 16;

 < /c90>  < c110>   < c128> Pagedatasource.currentpageindex = pager–1;

 < /c12>   < /c33>  < /c54> this. Repeater1.datasource = Pagedatasource;

 < /c66>  < /c87>  < /c107> this . Repeater1.databind ();

Because Pagedatasource is to read all the data, and then pagination, all suitable for some small sites, such as enterprise stations, the number is not very large use, about 2w of data use

Here are two common ways of paging, and here's a look at the usage of the paging view Control Aspnetpager:

Aspnetpager belong to a third party control, that is, it is not developed by Microsoft, and it is not developed by you, but by other technicians themselves, sharing the controls we use, so when we use it, we first download it from the Internet, and then, like other controls, we first put it in the toolbox, Here you can drag it directly in, or you can right-click the Tool menu item---Select items, and find the directory add in

Let's say a few of its properties and methods

alwaysshow : Indicates whether the control is displayed or not displayed at the current time on only one page

PageSize : The number of each page to display

RecordCount : total number

numericbuttoncount : Number of page pages to display

Note : The above three properties are the views used to display the paging control. But they just want to show the view of the paging control.

 < /c51>firstpagetext="firstpage"

lastpagetext= " last page"

nextpagetext= "Next Page"

 < /c12>prevpagetext= "Previous page"

pagingbuttonspacing= " 12 ": Width between each page number

numericbuttontextformats tring= ' [{0}] ': Style of page number display

enableurlrewriting= " True ': Do you want to enable URL rewriting, which is a domain name by default? Page= page Number

 < /c58> such as: http://www.hnzbtb.com/NewList.aspx?page=3, but you can change it again

Urlrewritepattern: Overridden URL, such as: Aspnetpager1.urlrewritepattern = "newlist.aspx?dd={0}"

Note: The point to be mentioned here is that if you want to change the argument to display the page number, you must modify the display and then modify the parameters in the URL

 < /c12>urlpaging= "True": whether to display pagination only with a URL pass parameter, if False, the URL is unchanged, and the status bar displays

submitbuttontext= " go" Showinputbox= "Always": Used to resolve the display of the Go button

Two common methods

1, pagechanging: Before the paging operation, so to get the current control page number must be in the method to assign values, Otherwise Aspnetpager1.currentpageindex will always get the current page value is 1, because it is equivalent to you do not click on any page number, from the beginning is 1, so you click on that page, the previous is 1

protected void Aspnetpager1_pagechanging (Object src, Wuqi.Webdiyer.PageChangingEventArgs e)

 < /c50> {

 < /c12>   < /c33>  < /c54> Pager = e.newpageindex-1;

 < /c66>  < /c87>  < /c107> databindnews ();

}

public void Databindnews ()

 < /c12> {

 < /c34>  < /c55>  < /c76> pageddatasource pagedatasource = new PagedDataSource ();

 < /c90>  < c110>   < c128> Pagedatasource.datasource = Newsmanager.getnews ();

 < /c12>   < /c33>  < /c54> pagedatasource.allowpaging = true;

 < /c66>  < /c87>  < /c107> aspnetpager1.recordcount = Newsmanager.getnewscount ();

 < /c12>   < /c33>  < /c54> pagedatasource.pagesize = 16;

 < /c66>  < /c87>  < /c107> pagedatasource.currentpageindex = Pager;

 < /c12>   < /c33>  < /c54> this. Repeater1.datasource = Pagedatasource;

 < /c66>  < /c87>  < /c107> this . Repeater1.databind ();

 < c121> }

The reason to subtract 1 is because the paging method and the Pagedatasource are all starting from 0.

Custom paging in Grilview, we'll use it too.

 < /c12> protected void Gridview1_pageindexchanging (object sender, Gridviewpageeventargs e)

 < /c42> {

 < /c66>  < /c87>  < /c107> this . Gridview1.pageindex = E.newpageindex;

 < /c12>   < /c33>  < /c54> databindproduct (0, NULL);

}

2, pagechanged: Click the operation after the page number, so you can directly through the aspnetpager1.currentpageindex–1 to get the number of pages in the current page, because you did click on the page numbers, it is clear that the current number of page numbers

 < /c74>  < /c95> protected void Aspnetpager1_pagechanged (Object Src,eventargs e)

 < /c12> {

 < /c34>  < /c55>  < /c76> databindnews ();

}

public void databindnews ()

 < /c98> {

 < /c12>   < /c33>  < /c54> PagedDataSource pagedatasource = new PagedDataSource ();

 < /c66>  < /c87>  < /c107> pagedatasource.datasource = Newsmanager.getnews ();

 < /c12>   < /c33>  < /c54> pagedatasource.allowpaging = true;

 < /c66>  < /c87>  < /c107> aspnetpager1.recordcount = Newsmanager.getnewscount ();

 < /c12>   < /c33>  < /c54> pagedatasource.pagesize = 16;

 < /c66>  < /c87>  < /c107> pagedatasource.currentpageindex = aspnetpager1.currentpageindex–1;

 < /c12>   < /c33>  < /c54> this. Repeater1.datasource = Pagedatasource;

 < /c66>  < /c87>  < /c107> this . Repeater1.databind ();

 < c121> }

If Aspnetpager is used to page, the current page is specified with Aspnetpager as long as the total number is assigned to Aspnetpager, and the number of displayed per page is guaranteed Aspnetpager and pagedatasource the specified consistency can implement paging. Simply put, this page control does not use ViewState to save, pages and page display records directly through the ASP.net to control the line

Need to note that there are some things that need to be consistent, if not consistent, it may lead to uncoordinated, such as the 4 pages, but aspnetpager only show 2 pages, these need attention!

Here's a quick way to share a style for pagination controls:

CSS style:


. Pages
{
  color: #999;
}
. Pages a,. pages. CPB
{
  Text-decoration:none;
  float:left;
  padding:0 5px;
&NBSP border:1px solid #075198;
  background: #ffff;
  margin:0 2px;
  font-size:11px;
  color: #000;
}
. pages a:hover
{
  background-color: #075198;
  color: #fff;
&NBSP border:1px solid #075198;
  text-decoration:none;
}
. Pages. CPB
{
  font-weight:bold;
  color: #fff;
  background: #075198;
&NBSP border:1px solid #075198;
}

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.