Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 654361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:29:09+00:00 2026-05-13T22:29:09+00:00

ok i have a project which has many gridview in its pages… now i

  • 0

ok i have a project which has many gridview in its pages… now i am sorting the fridveiw using the sorting function like this:

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = Session["TaskTable2"] as DataTable;

        if (dt != null)
        {

            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            GridView1.DataSource = Session["TaskTable2"];
            GridView1.DataBind();
        }

    }

    private string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection2 = "ASC";

        // Retrieve the last column that was sorted.
        string sortExpression2 = ViewState["SortExpression2"] as string;

        if (sortExpression2 != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression2 == column)
            {
                string lastDirection = ViewState["SortDirection2"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection2 = "DESC";
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection2"] = sortDirection2;
        ViewState["SortExpression2"] = column;

        return sortDirection2;
    }

but this code is being repeated in many pages so i tried to put this function in a C# class and try to call it but i get errors….

for starters i get the viewstate error saying 😐

“viewstate does not exist in the current context”

so how do i go about doing this ….??

thanks

so this is what is there in my class:

public string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection2 = "ASC";

        // Retrieve the last column that was sorted.
        string sortExpression2 = ViewState["SortExpression2"] as string;

        if (sortExpression2 != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression2 == column)
            {
                string lastDirection = ViewState["SortDirection2"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection2 = "DESC";
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection2"] = sortDirection2;
        ViewState["SortExpression2"] = column;

        return sortDirection2;
    }

and i am calling it from my code like this:

 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = Session["TaskTable2"] as DataTable;

        if (dt != null)
        {

            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + new impersonateClass().GetSortDirection(e.SortExpression);
            GridView1.DataSource = Session["TaskTable2"];
            GridView1.DataBind();
        }

    }

and i get view state error…

is here a way to put this entire thing in the class… because it is getting repeated everywhere…

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:29:09+00:00Added an answer on May 13, 2026 at 10:29 pm

    You will need to pass the ViewState in, as the ViewState object is a member of the Page class. Once you moved the code into a separate class, it no longer had access to the ViewState object.

    public string GetSortDirection(string column, StateBag viewState) {
        // Your code here.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User model which has many projects and a Project model which
In a Rails 3.2 app I have a model Project, which has many Tasks.
I have been given new project which has many classes and I have to
In a project here, we have an Object ClassTest, which has many sub objects:
Lets say I have a project model which has many members and many tasks.
I am working for a project which has many XSLT transformations. The transformations have
I have a solution which has many projects inside it. I build the solution
I have a project which has a set of binary dependencies (assembly dlls for
I have a project which has shared functionality between three other projects and have
I have one project which has RIAService with entity framework that is referenced to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.