Maintaining GridView current page index after navigating away from Gridview page

前端 未结 4 1683
-上瘾入骨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:46

    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

    0 讨论(0)
  • 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
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-18 13:53

    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;
    }
    
    0 讨论(0)
  • 2020-12-18 14:08

    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"]));
                }
            }
    
    0 讨论(0)
提交回复
热议问题