Maintaining GridView current page index after navigating away from Gridview page

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, such that a user can navigate to another page (details page) from the list. On the details page, they have “Back” button to return to the GridView page

Edit
Just to clarify the query

I am looking for sample code snippet on the Server Side on how to specify the page index to set the GridView after data binding. The idea is to ensure the user navigates to the same page index they were on.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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
        }
    }
}

Method 2

I’m more of a fan of the Session approach, personally. Simply save your page index as a session variable, and, if this Session variable isn’t null on page load, use it to fire your “OnPageIndexChanging” method, like so:

Set your current page number whenever the page number changes:

    protected void GridViewIndexChanging(object sender, GridViewPageEventArgs e)
    {
        myGridView.PageIndex = e.NewPageIndex;
        Session["pageNumber"] = e.NewPageIndex;
        //whatever your page index changing does...
    }

Then, on Page_Load do something like:
        if (!IsPostBack)
        {
            if (Session["pageNumber"] != null)
            {
                GridViewIndexChanged(myGridView, new GridViewPageEventArgs((int)Session["pageNumber"]));
            }
        }

Method 3

you can ues the Page Index Change Event of Gridview and Find out the Current Page Index for e:g

yourGridId.PageIndex=e.NewPageIndex;
ViewState["GridPageIndex"]=e.NewPageIndex;

on PageLoad Get the Viewstate Value
string pIndex=string.Empty;
pIndex=Convert.toInt32(ViewState["GridPageIndex"]);
if(!string.Empty(pIndex))
{
yourGridId.PageIndex =pIndex;
}

Method 4

you must use query string and is recommended, otherwise you can use session object but don’t use that for this as you may have grid view opening in different pages so use query string .

gridView1.CurrentPageIndex = (Request["pageNo"] != null) ? Request["pageNo"] as int : 0; 
gridView1.DataSource = myDataSet;
gridView1.DataBind();

you can update your link on GridView_DataBound event


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x