logo
  • Jobs
  • About Me
  • Contact
  • Home
« The Bell Ringer
No one individual is greater than the team »

AllowSorting and ViewState

Posted November 3rd, 2005 by Matt Berther

Im working through a bug fix round on an ASP.NET project that I’ve had for a while. One of the issues that we found was that all of the sudden, when turning ViewState off, the Sort functionality stopped working on a particular datagrid that we had. We had to leave ViewState off, because of the sheer amount of data that was being put into the grid was causing timeouts when the page was loading and other issues.

Interestingly enough, it seems that if you turn the view state off, you need to rebind the grid to get sorting to work. Ultimately, this means that you’re binding the grid twice.

I had all my code inside of an if (!Page.IsPostBack) block, so the fix was simple enough. Im just wondering about performance. Is it that much more expensive to bind twice?

3 Comments

This entry was posted on Thursday, November 3rd, 2005 at 3:51 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Milan Negovan
November 4th, 2005

We’ve been dealing with this pain for a long time now, so I can very well relate to your story. I’m afraid if you want a sorting and paging DataGrid you have to enable view state, and yes, you have to bind twice on postback whenever you sort or page it. It’s inefficient, but I don’t think there’s a way around it.

In my article on view state (pardon this shameless plug) I wrote:

“Remember, DataGrid renders as a table with rows and cells being its child controls. View state is maintained for these child controls. The DataGrid has no memory of the data source you bound it to! The datasource is NOT stored in the viewstate.” (http://www.aspnetresources.com/articles/ViewState.aspx)

Also, a couple of years ago Dino Esposito wrote:

“Doing without view state is difficult whenever state information can’t be dynamically inferred. For example, if you want to track the sort order of a pageable DataGrid you can cache it only in the view state” (http://msdn.microsoft.com/msdnmag/issues/03/02/CuttingEdge/)

I’m a big proponent of cutting view state to the bone, but DataGrid controls don’t cooperate that well.

Jim Gonzales Omaha NE
August 24th, 2006

Here is how I’m fixing it

At the bottom of my sort sub I have some code like

ViewState(”ImSorting”) = “True”

Then in my pageload I have on the post back

If CStr(ViewState(”ImSorting”)) = “True” Then
ViewState(”ImSorting”) = “False”
Else
BindGrid()
End If

The sort goes first binds the grid and flags it as not needing to be bound and all the postback does is reset the flag. This gets rid of the extra bind.

Jim Gonzales Omaha NE
August 24th, 2006

I got to thinking about it after I posted and came up with an even easier way to prevent the extra bind.

Use some code like this in your post back

If Request.Form.Get(”__EVENTARGUMENT”) = “” Then
BindGridFunction()
End If

Should be the only code you need. This property is going to be empty if there is no sorting / paging going on. If there is something in there then you must of sorted or paged or something.

If your grid has a lot going on you might have to look at the argument to see what you should do.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010