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 6144159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:36:45+00:00 2026-05-23T18:36:45+00:00

I have an ASP .NET page with several GridView controls for which I’m implementing

  • 0

I have an ASP .NET page with several GridView controls for which I’m implementing sorting and paging.

I’m using session variables to maintain a DataTable representing a given page of the GridView data in the ViewState like so:

protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
      // Session["Page"] represents the active page of the GridView 
      // when the Sorting event fires.
      DataTable dt = Session["Page"] as DataTable;

      if (dt != null)
      {       
           if (ViewState["SortDirection"] == null)
           {
                ViewState["SortDirection"] = "DESC";
           }

           string ViewState_SortDirection = ViewState["SortDirection"].ToString();

           for (int i = 0; i <= ((GridView)sender).Columns.Count - 1; i++)
           {
                if (e.SortExpression == ((GridView)sender).Columns[i].SortExpression)
                {
                     if (ViewState["SortDirection"].ToString() == "ASC")
                     {
                          e.SortDirection = SortDirection.Descending;
                          ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + " ▼";
                          ViewState["SortDirection"] = "DESC";
                     }
                     else if (ViewState["SortDirection"].ToString() == "DESC")
                     {
                          e.SortDirection = SortDirection.Ascending;
                          ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + " ▲";
                          ViewState["SortDirection"] = "ASC";
                     }
                }
           }

           DataView dv = new DataView(dt)
           {
                Sort = e.SortExpression + " " + ViewState["SortDirection"]
           };

           gv.DataSource = dv;
           gv.DataBind();

           Session["Page"] = dv.ToTable();
           DataTable dt = Session["Page"] as DataTable;
      }
 }

I’d like to have each GridView use the same Sorting event handler. When session variables in the state bag like Session[“Page”] are in use, is this session variable specific to the GridView whose Sorting event fires? Or can it be modified by other GridView controls using it to sort on the same page? Meaning, if I have another GridView which also uses Session[“Page”] for paging, will that session variable be in that control’s scope?

Or, should I just follow the lead of this post’s answer and pass only the SortDirection for each session?

  • 1 1 Answer
  • 0 Views
  • 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-23T18:36:45+00:00Added an answer on May 23, 2026 at 6:36 pm

    Here are the available data-persisting variable types and their scopes:

    • Application Variables (shared between all user sessions, for the entire application)
    • Session Variables (shared between all pages, for the entire user session)
    • ViewState Variables (exists solely on the page, for the entire page)
    • ControlState Variables (exists solely on the page, just for the control)

    If you are storing your grid data as Session variables, you will run into issues when you use two grids in the same page. I cannot give any specific suggestions on how to store the data without knowing more about the system, but the types of variables above may point you in the right direction

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an asp.net page within which I've used several AJAX controls doing partial
I have this page which has several TextAreas (asp.net mvc). I need to validate
I have several <li> elements with different id's on ASP.NET page: <li id=li1 class=class1>
I have an ASP.NET page which has a script manager on it. <form id=form1
I have a ASP.Net page using ADO to query MS access database and as
I have an ASP.NET page which has a button it it. The button click
I have an asp.net page with several list boxes. I would like to include
We have an ASP.NET page which uses an update panel for partial page postbacks.
On one of my ASP.NET pages I have several asp:textbox fields. On this page
I have an ASP.NET code-behind page linking several checkboxes to JavaScript methods. I want

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.