I’m using a standard GridView with a LinqDataSource. The user can sort and page through the grid using the stock standard stuff. Searching criteria (WhereParameters) can also be used to filter the results. This works great, but the state is obviously lost whenever navigating away from the page.
So a generic mechanism of capturing the Sort and Pagining state as well as the WhereParameter values would be great. One would then be able to add these values to a Session and restore them whenever the user navigates back to the page.
Any help would be much appreciated.
My code as follows:
<asp:GridView ID="dataGridView" runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
CssClass="GridView"
DataSourceID="linqDataSource"
PageSize="20">
<Columns>
<asp:BoundField
HeaderText="Name"
DataField="Name"
SortExpression="Name" />
<asp:TemplateField
HeaderText="Province"
SortExpression="Province.Name">
<ItemTemplate>
<%# Eval("Province.Name")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="linqDataSource" runat="server"
ContextTypeName="DataContext"
TableName="Schools"
EnableUpdate="True"
EnableDelete="True"
Where="Name.Contains(@Name) && (ProvinceID == @ProvinceID)">
<WhereParameters>
<asp:ControlParameter
Name="Name"
DefaultValue=""
ControlID="tbName"
Type="String"
ConvertEmptyStringToNull="False" />
<asp:ControlParameter
Name="ProvinceID"
DefaultValue=""
ControlID="ddlProvince"
Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
Andy,
Have you considered hooking into the gridviews OnDataBound Event and storing the appropriate gridview attributes in a cookie? You can then tap that cookie on Non PostBack PageLoad event to restore the gridview to approximately* where the user left it.
You can use the cookie expiration property if you want the page to remember its state for a long time.
anyway…Here is some sample code.
Maybe there is a better way than a cookie.
I welcome and critique and feedback from the community. I consider myself a .net newb, so if anyone sees potential issues or danger, please offer up your thoughts.
Mike