CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Sorting in a datagrid without using the viewstate

The datagrid has an option to sort the grid on a column. Every column in the grid has has SortExpression property. When you click the column header the SortCommand event is fired. In the parameters of that event you can get the SortExpression of the column whose header was clicked.

That’s very nice but the moment you disable the grid’s viewstate the event no longer fires. The viewstate of a datagrid gets very big, it’s something you want to keep disabled. But the sorting option is nice. I found the folowing workaround.

When the page posts back it is not a great problem to find out which control caused the postback. The names of the sortlinkbuttons all begin with the name of the datagrid. The second part of the name is the grid’s header and part of the third name is the column. So you can check (in page_prerender,or page_load) if and which column was clicked

// In case of column header click
// Request.Form["__EVENTTARGET"] will look like "DataGrid1:_ctl1:_ctl3"

char[] splitters = {':'};
string[] pbCtrl = Request.Form["__EVENTTARGET"].Split(splitters);

if (pbCtrl[0] == DataGrid1.ID)
{
    // Column ID's is 1-based !
    int columnClicked = int.Parse(pbCtrl[2].Substring(4)) + 1;
    sortOrder = DataGrid1.Columns[columnClicked].SortExpression;
}

The SortExpression is not in the viewstate and fully available.

It is a hack. But works like a charm.


Published Mar 01 2005, 12:51 PM by pvanooijen
Filed under:

Comments

Darrell said:

Nice.
# March 1, 2005 12:06 PM

Brendan Tompkins said:

"That’s very nice but the moment you disable the grid’s viewstate the event no longer fires."

Peter, are you sure about this? I often turn off ViewState for my grids, and handle the SortCommand event all the time.

A problem I could see is parsing out something that these form values, is that they really should be viewed as semi-private members of the control. It's think kind of thing, that I think had they been able to, they would have encapsultated.

If the format of the control name changes, your code will break!

# March 1, 2005 1:46 PM

Brendan Tompkins said:

"A problem I could see is parsing out something that these form values, is that they really should be viewed as semi-private members of the control. It's think kind of thing, that I think had they been able to, they would have encapsultated."

Geez. Could my english have been any worse? Let me re-type that.

A problem I could see with parsing out these form values, is that they really should be viewed as semi-private members of the control. It's the kind of thing, that I think had they been able to, they would have encapsulated.
# March 1, 2005 2:16 PM

Peter van Ooijen said:

"The view state breaks the event in my control"
Besides that I found several resources on the Web on diverse events failing.
It is likely to break in another Net version. But there we will have controlstate.
# March 1, 2005 2:20 PM

Peter van Ooijen said:

And a better grid control.
# March 1, 2005 2:24 PM

Peter's Gekko said:

The Datagrid is a lovely control when you need a web page whose layout has to adapt to a a huge variety...
# March 23, 2005 4:16 AM

Oliver Crow said:

Brendan Tompkins wrote: "Peter, are you sure about this? I often turn off ViewState for my grids, and handle the SortCommand event all the time. "

My experience confirms Peter's assertion ... setting EnableViewState="false" in the DataGrid also prevents the DataGrid SortCommand events from firing. At least that's what I see in the VS.Net debugger.

However there is a way to reduce the viewstate size for large grids without disabling the sorting and paging functionality. The DataGrid control contains an DataGridItem control for each row of the grid. Turning off the viewstate on these controls prevents the size of the viewstate from scaling linearly with the number of rows in the grid.

To do this catch the grid's ItemDataBound event and turn off the view state of the referenced control. In C#:

private void testGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {
e.Item.EnableViewState = false;
}
# January 3, 2006 4:37 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News