Question

I have a grid created using a sql datasource in my asp page. So Sorting is done automatically using asp.net in build control(no codes added for sorting in vb.net).

But If I refresh the page or if I go to some other page, the sorting order disappears.

Is there a way to catch the previous sort order (sort direction and sort expression)?

Was it helpful?

Solution

On page PreRender event store the current values of GridView.SortDirection and GridView.SortExpression properties in, for example, Session:

this.Session[this.GetType().FullName + ".GridView.SortDirection"] = this.GridView.SortDirection;
this.Session[this.GetType().FullName + ".GridView.SortExpression"] = this.GridView.SortExpression;

On page Init event execute this code:

if (!this.IsPostBack && this.Session.Contains(this.GetType.FullName + ".GridView.SortDirection"))
{
    this.GridView.SortDirection = (SortDirection)this.Session[this.GetType().FullName + ".GridView.SortDirection"];
    this.GridView.SortExpression = (string)this.Session[this.GetType().FullName + ".GridView.SortExpression"];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top