So I have a method that I “borrowed” from the internet that I use on many of my pages to handle the sorting of a grid column.
private string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
Now it works great, but alas, I must copy it to every page that I want to call it from because it references the viewstate. So I want to move it to my helper class and have it store in the session state instead, however, I can referernce neither State in the helper class.
Is their any way to access the session from a help class? Can I pass by reference the session state?
If I understand the question correctly, you’re looking to access
ViewStateorSessionfrom a class that is not a page.If so, you can use
HttpContext.Current.Session, or, you should be able to typecastHttpContext.Current.CurrentHandlerto typePage, and then access theViewState.Alternatively, you could always just put your code in a base class, which all your pages will inherit from, rather than inheriting from
System.Web.UI.Page.