Bidirectional Sorting Without ViewState Enabled
page 1 of 1
Published: 20 Sep 2003
Unedited - Community Contributed
Abstract
When using a datagrid filled with statistics, it is useful to be able to sort the columns of the datagrid in ascending or descending order. Switching the sort direction of a column requires keeping track of the column that was last sorted and the direction it was sorted. Typically this is done by using datagrid custom attributes, which use ViewState, or by directly using custom ViewState properties. Using ViewState is fine for a fairly small datagrid. However, with a medium or large-sized datagrid, the amount of ViewState data becomes large very quickly.
by Daniel Olson
Feedback
Average Rating: 
Views (Total / Last 10 Days): 17334/ 20

When using a datagrid filled with statistics, it is useful to be able to sort the columns of the datagrid in ascending or descending order.  Switching the sort direction of a column requires keeping track of the column that was last sorted and the direction it was sorted.  Typically this is done by using datagrid custom attributes, which use ViewState, or by directly using custom ViewState properties.  Both of these methods are basically the same.  For an example of the first technique, see Effective Sorting in ASP.Net DatatGrids, or, for the second technique, see Sorting XML Data Using the .Net DataGrid

Using ViewState is fine for a fairly small datagrid.  However, with a medium or large-sized datagrid, the amount of ViewState data becomes large very quickly.  The tables below show a comparison of the html output of a page having a medium-sized datagrid when the ViewState is enabled versus when it is not. 

Page With ViewState Enabled

  Total ViewState Non-ViewState
Characters

20,696

14,875

5,821

Percent

100

72

28


Page Without ViewState Enabled

  Total ViewState Non-ViewState
Characters

5,920

99

5,821

Percent

100

2

98

The page that has a ViewState-enabled datagrid is almost four times larger than the page would be without it enabled.  The page that has a ViewState-enabled datagrid will clearly take longer to render.  When doing bidirectional sorting in a datagrid, ViewState is not necessary to keep track of the column and the direction.  This information can be stored other ways.  The code below shows an example of bidirectional sorting using session variables which eliminates the need for ViewState.

View Live Demo

View aspx Source

View aspx.cs Source

View XML Source

For another example of how ViewState could be avoided, see ASP.Net and Databases Part 7.  This article, while not discussing ViewState, shows an example of using a radio button to indicate the direction of the sort.  This method makes the use of ViewState unnecessary.

Of course, there may be good reasons to keep ViewState enabled.  Without ViewState, the datagrid loses other capabilities, such as paging or using the edit column.  Also, without ViewState, the datagrid has to be bound twice, first when the page loads and then again when the OnSort event fires.

However, in order to quickly render a medium-to-large datagrid that only needs sorting capabilities, it is worth considering disabling ViewState and using another method to hold sorting information.



User Comments

Title: MR.   
Name: Ajay
Date: 2006-02-23 5:37:04 AM
Comment:
Good one. Quite impressive. I will try implementing it in my live project. Thanks alot.
Title: Mr   
Name: Dave Taylor
Date: 2005-10-26 9:44:48 AM
Comment:
You bind the datagrid twice!
Title: WHERE IS THE BI-SORT???   
Name: Patrick
Date: 2005-07-17 10:36:58 PM
Comment:
This code doesn´t make BiDirectional SORT, because when the Page_Load rises, it calls the SetdgAthletes();, so far so good, but when you make anu sort, it calls the dgAthletes_SortCommand that calls by it self SetdgAthletes();, but after it´s done, the page call´s again the SetdgAthletes();, making sort, but ONLY ASC way, it nevers hit the DESC, in the dgAthletes_SortCommand it hit´s the DESC way but this source is killed by the second time you call SetdgAthletes(); .

You should test it before. After this problem, you will have the problem that is when some body want´s a Grid withOut ViewState but he/she doens´t want to load it in the Page_Load, but by a click in a button.

Patrick
*p*a*t*r*i*c*k*.*a*n*t*o*n*i*o*l*i*@g*m*ai*l*.*c*o*m*
Title: Horrible Article   
Name: jeremiah mcelroy
Date: 2004-10-12 8:48:44 AM
Comment:
Merely providing a set of files and a brief description of their contents does not make for an an article or tutorial. This is simply a glorified code snippet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 10:25:37 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search