Home All Groups Group Topic Archive Search About
Author
21 Feb 2006 12:47 PM
Larry Charlton
Is there a way to selectively turn off viewstate for a datagrid?

I've implemented server side paging for the grid with an object datasource,
and there's no reason to track the actual data in the grid in viewstate, but
I still need to track the page and sort order.

Author
22 Feb 2006 1:08 AM
Phillip Williams
If you re-databind upon each postback the sort and paging would work.  You
can also manage the sort and the paging by custom coding.  For example, to
sort the datagrid all you need to know is what key the user wanted to sort
by.  You can then have the HeaderText as an anchor tag that triggers a click
event of a Button with its commandArgument value set as the desired key for
sorting.

Inside the datagrid you would have in the column a definition for
headerTemplate:

<HeaderTemplate>
    <a href="javascript:SortBasedOn('Field1')">Field1</a>
</HeaderTemplate>

At the buttom of the webform you would add:

<asp:Button CssClass="Hidden" EnableViewState="True" Runat="server"
CommandArgument="Field1" OnCommand="datagrid1_Sort" CommandName="Sort"
ID="btnField1"></asp:Button>

Inside your Javascript section you would add:

function SortBasedOn(KeyWord)
{
    var btn= findControl("btn"+KeyWord);

    if (btn!=null)   
    {
        btn.click();
    }
}

And in the codebehind you would have a datagrid1_Sort function that responds
to the button command event to recreate a dataview sorted based on the new
sort key and databind it to the datagrid.

Show quoteHide quote
"Larry Charlton" wrote:

> Is there a way to selectively turn off viewstate for a datagrid?
>
> I've implemented server side paging for the grid with an object datasource,
> and there's no reason to track the actual data in the grid in viewstate, but
> I still need to track the page and sort order.