views:

1012

answers:

2

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>
A: 

Well, the ugly way would be to use the session and give it either majic strings or define somewhere the value for them

Session["_page"] = _currentPage;
Session["_sortColumn"] = _currentSortColumn;
Session["_sortDir"] = _currentSortDir;

Or you could get tricky and stick it in the user profile. Then load the values up on a page_load or something like that. The downside is that if anyone navigates away, comes back a half hour later, they are back on the page, etc that they were last at. If you are looking for them to navigate away, like to a detail form, then back, just pass the values to the next page. Then you can return back with the values on the query string or something like that.

Enjoy.

Mike
Thanks for the answer, but not really what I'm after. I need some generic way of hooking up the OnSorting and OnPageIndexChanging methods to save the sorting and paging info. I also need a way to store the WhereParameters. When a user returns to the page, I need a method to restore these saved vales.
Andy
A: 

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.

  • Approximately because the gridview may be a bit different based on any CRUD operations on the source data occuring from other activity while the user was away.

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.

protected void gvCountryList_DataBound(object sender, EventArgs e)
{
  // Store gvCountryList Gridview Sort, PageIndex, and PageSize info 
  //   in a cookie so that we can redisplay same grid position next 
  //   time they visit page. 
  HttpCookie aCookie = new HttpCookie("MaintainCountries");
  aCookie["SortExp"] = gvCountryList.SortExpression.ToString();
  aCookie["SortDir"] = gvCountryList.SortDirection.ToString();
  aCookie["PageIndex"] = gvCountryList.PageIndex.ToString();
  Response.Cookies.Add("aCookie");
)

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == false)
    {

        //Grab the cookies
        if (Request.Cookies["MaintainCountries"] != null)
        {
            if ((Request.Cookies["MaintainCountries"]["SortExp"] != null)
             && (Request.Cookies["MaintainCountries"]["SortDir"] != null)
             && (Request.Cookies["MaintainCountries"]["PageIndex"] != null))
            {
                SortDirection srtDir = SortDirection.Ascending;
                if (Request.Cookies["MaintainCountries"]["SortDir"].ToUpper() == "DESCENDING")
                    srtDir = SortDirection.Descending;
                gvCountryList.Sort(Request.Cookies["MaintainCountries"]["SortExp"].ToString(), srtDir);
                gvCountryList.PageIndex = Convert.ToInt32(Request.Cookies["MaintainCountries"]["PageIndex"].ToString());
            }
        }
    }
}

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

MikeSullivan
looks promissing! thanks for the answer. a bit swamped at the moment, but I'll try it soon and get back to you.
Andy