2

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.

Julius A
  • 38,062
  • 26
  • 74
  • 96

4 Answers4

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"]));
            }
        }
LeeCambl
  • 376
  • 4
  • 15
2

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
        }
    }
}
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • You forgot ViewState[] – Fandango68 Mar 29 '17 at 07:01
  • @Fernando68: ViewState will only work if the link does not actually leave the page. OP is asking for a solution to which a link can be clicked and when the user returns to the page the same index is applied. ViewState will not accomplish this. – Joel Etherton Mar 29 '17 at 19:35
  • If the GridView is inside an UpdatePanel, ViewState is enough as there is no overall PostBack. Sorry but that's how I got it to work for me. I prefer not to keep data throughout the life of a Session. – Fandango68 Mar 30 '17 at 01:23
  • @Fernando68: OP explicitly states the purpose is to go to another page, not an UpdatePanel. `such that a user can navigate to another page`. While your point is correct, ViewState will work for an UpdatePanel, this question is explicit in requiring an exit of the current page. ViewState will not allow for this appropriately. – Joel Etherton Mar 31 '17 at 14:23
1

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;
}
ash060
  • 137
  • 2
  • 4
  • 14
  • on Page_Load the ViewState["GridPageIndex"] is null. You should use Session["GridPageIndex"] if you want the PageIndex to have a any value on Page_Load – Nader Khazai Mar 07 '20 at 08:00
0

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

Adeel
  • 19,075
  • 4
  • 46
  • 60