ASP. Net continues sorting after turning pages

Source: Internet
Author: User

To continue sorting after turning pages, this effect is achieved:
For example, a total of 15 records are displayed, and 10 records are displayed on each page.
Sort the first 10 records on the first page, and then sort the last five records on the second page.

Note the following:
1. If there is a lot of data, it is best not to put the dataset into the cache (viewstate), affecting performance

2. The ViewState stores the last e. expression and whether the e. expression is in ascending or descending order.

Example:
1. The existing sorting event is written in this way. This is used when you click the above sorting title:
Private void grdProjTrace_SortCommand (object source, DataGridSortCommandEventArgs e)
{
This. grdProjTrace. CurrentPageIndex = 0;
DataView dv = get data code;
String strSort = "";
String strOrder = ""; // sorting method. 0, descending, 1 ascending
If (ViewState ["SortExpresstion"]! = Null)
{
StrSort = ViewState ["SortExpresstion"]. ToString ();
StrSort = strSort. Substring (0, strSort. Length-1 );
StrOrder = ViewState ["SortExpresstion"]. ToString ();
StrOrder = strOrder. Substring (strOrder. Length-1 );
}
If (e. SortExpression = "CustomerName ")
{
If (strSort! = "CustomerName ")
{
This. ViewState ["SortExpresstion"] = ustomerName0 ";
Dv. Sort = "CustomerName DESC ";
}
Else
{
If (strOrder = "0 ")
{
This. ViewState ["SortExpresstion"] = "CustomerName1 ";
Dv. Sort = "CustomerName ASC ";
}
Else
{
This. ViewState ["SortExpresstion"] = "CustomerName0 ";
Dv. Sort = "CustomerName DESC ";
}
}
}
If (e. SortExpression = "fullName ")
{
If (strSort! = "FullName ")
{
This. ViewState ["SortExpresstion"] = "fullName0 ";
Dv. Sort = "fullName DESC ";
}
Else
{
If (strOrder = "0 ")
{
This. ViewState ["SortExpresstion"] = "fullName1 ";
Dv. Sort = "fullName ASC ";
}
Else
{
This. ViewState ["SortExpresstion"] = "fullName0 ";
Dv. Sort = "fullName DESC ";
}
}
}
This. grdProjTrace. DataSource = dv;
This. grdProjTrace. DataBind ();
}

2. The following method is self-written and called in the page flip event.
Private void ChangePageDataBind ()
{
DataView dv = get data code;
String strSort = "";
String strOrder = ""; // sorting method. 0, descending, 1 ascending
If (ViewState ["SortExpresstion"]! = Null)
{
StrSort = ViewState ["SortExpresstion"]. ToString ();
StrSort = strSort. Substring (0, strSort. Length-1 );
StrOrder = ViewState ["SortExpresstion"]. ToString ();
StrOrder = strOrder. Substring (strOrder. Length-1 );
}
If (this. ViewState ["SortExpresstion"]! = Null)
{
If (strSort = "CustomerName ")
{
If (strOrder = "1 ")
{
This. ViewState ["SortExpresstion"] = "CustomerName1 ";
Dv. Sort = "CustomerName ASC ";
}
Else
{
This. ViewState ["SortExpresstion"] = "CustomerName0 ";
Dv. Sort = "CustomerName DESC ";
}
}
}
If (this. ViewState ["SortExpresstion"]! = Null)
{
If (strSort = "fullName ")
{
If (strOrder = "1 ")
{
This. ViewState ["SortExpresstion"] = "fullName1 ";
Dv. Sort = "fullName ASC ";
}
Else
{
This. ViewState ["SortExpresstion"] = "fullName0 ";
Dv. Sort = "fullName DESC ";
}
}
}
This. grdProjTrace. DataSource = dv;
This. grdProjTrace. DataBind ();
}

You can directly call the above two methods by modifying the field names to be sorted.
1. This method is easy to use.
The 2 method is used as follows:
Private void grdProjTrace_PageIndexChanged (object source, DataGridPageChangedEventArgs e)
{
Try
{
Try
{
This. grdProjTrace. CurrentPageIndex = e. NewPageIndex;
}
Catch
{
This. grdProjTrace. CurrentPageIndex = 0;
}


This. ChangePageDataBind ();
}
Catch (System. Exception errWS)
{
// Exception
}
}

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.