Maintaining GridView current page index after navigating away from Gridview page

前端 未结 4 1707
-上瘾入骨i
-上瘾入骨i 2020-12-18 13:27

I have a GridView on ASP.NET web form which I have bound to a data source and set it to have 10 records per page.

I also have a hyper link column on the GridView,

4条回答
  •  余生分开走
    2020-12-18 13:50

    The three basic options at your disposal: query string, session, cookie. They each have their drawbacks and pluses:

    1. Using the query string will require you to format all links leading to the page with the gridview to have the proper information in the query string (which could end up being more than just a page number).
    2. Using a session would work if you're sure that each browser instance will want to go to the same gridview, otherwise you'll have to tag your session variable with some id key that is uniquely identifiable to each gridview page in question. This could result in the session management of a lot of variables that may be completely undesirable as most of them will have to expire by timeout only.
    3. Using a cookie would require something similar where cookie data is stored in a key/data matrix (optimized hash table might work for this). It would not be recommended to have a separate cookie name for each gridview page you're tracking, but rather have a cookie with a common name that holds the data for all gridview pages being tracked and inside that have the key/value structure.

    Edit: A small code snippet on setting the page index.

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            try
            {
                if(HttpContext.Current.Request["myGVPageId"] != null])
                {
                    myGridview.PageIndex = Convert.ToInt32(HttpContext.Current.Request["myGVPageId"]);
                }
            }
            catch(Exception ex)
            {
                // log it
            }
        }
    }
    

提交回复
热议问题