asp.net to maintain sort after page turn

Source: Internet
Author: User
Tags expression sort tostring
Asp.net| Page | sort

To continue sorting after the paging is done, you will achieve this effect:

For example, a total of 15 records, each page shows 10

Then sort: The first page sorts the top 10 records, and then the second page is sorted after five.

The following points should be noted:

1. If the data is many, it is best not to put the dataset into the cache (ViewState), affect performance

2. ViewState which e.expression to store the last time and to store this e.expression is ascending or descending

Examples are as follows:

1. The existing sort event is written in this way by clicking on the above sort headers:

private void Grdprojtrace_sortcommand (object source, DataGridSortCommandEventArgs e)
{
This.grdProjTrace.CurrentPageIndex = 0;
DataView DV = Get data code;
String strsort = "";
String strorder = "";//Sort 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 written by itself, called in the paging event.

private void Changepagedatabind ()
{
DataView DV = Get data code;
String strsort = "";
String strorder = "";//Sort 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 ();
}

The above two methods can be invoked directly if you modify the name of the field to be sorted.

1, the method is very simple and practical, here will not say.

2, the method is this use:

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)
{
Abnormal
}
}




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.