Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page refresh Vs IsPostBack

Tags:

c#

.net

asp.net

I've got an index page which sends users to an edit products page on separate browser tabs.

For each products edited the index rewrites the Session["ProductID"].

The Edit page then has the following code to have a unique identifier for this tab and product:

if (!IsPostBack) //first time page load
{
    Random R = new Random(DateTime.Now.Millisecond + DateTime.Now.Second * 1000 + DateTime.Now.Minute * 60000 + DateTime.Now.Minute * 3600000);
    PageID.Value = R.Next().ToString();

    Session[PageID.Value + "ProductID"] = Session["ProductID"];
}

This works, and when the same user opens multiple tabs I only reference the Session[PageID.Value + "ProductID"] in my code so that I always have the proper ID. (I'm working in a trusted environment this is for an intranet, therefore I'm not too bothered with the level of security).

My issue occurs if the user does a page refresh by hitting the F5 key. At which point the Session[PageID.Value + "ProductID"] gets the Session["ProductID"] of the last product he opened.

For example:

User 1 opens product1 in tab1

User 1 opens product2 in tab2

Whenever they use the tool normally, everything works fine. However if:

User 1 on product1 page hits the refresh button (F5) the product1 page becomes product2 page

Is there a way to detect a page refresh from a "first load/redirect from another page" so that I can then tell my page not to update my Session[PageID.Value + "ProductID"]?

like image 469
LanFeusT Avatar asked Feb 25 '23 07:02

LanFeusT


2 Answers

Personally, I would go for URL parameters. E.g. pass the product IDs as URL parameters.

If you need the pages without parameters, you could e.g.

  1. Pass parameter to page.
  2. Page reloads itself if parameter is present and removes the parameter

This way you could distingues between first call (=parameter present) and second+ call (parameter not present).

like image 58
Uwe Keim Avatar answered Mar 07 '23 03:03

Uwe Keim


I've solved a very similar issue by storing two versions of a state-identifying parameter: one in Session and one in either the ViewState or the URL (QueryString).

If you compare the two values on Page_Load, that will tell you whether the session variable has changed since the page was first loaded. This should be just what you need.

EDIT: Rough sketch of the code (warning - haven't seen the actual code since I wrote it 3 years ago):

protected string currentProductID
{
    get
    {
        return Request.QueryString["ProductID"];
        //or: 
        //return (string)ViewState["ProductID"];
        //or:
        //return HiddenField1.Value;
    }
    set
    {
        Response.Redirect(ResolveUrl("~/MyPage.aspx?ProductID=" + value));
        //or:
        //ViewState.Add("ProductID", value);
        //or: 
        //HiddenField1.Value = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    //If the problem only occurs when not posting back, wrap the below in
    // an if(!IsPostBack) block. My past issue occurred on both postbacks
    // and page refreshes.

    //Note: I'm assuming Session["ProductID"] should never be null.

    if (currentProductID == null)
    {
        //Loading page for the first time.
        currentProductID = (string)Session["ProductID"];
    }
    else if (currentProductID != Session["ProductID"])
    {
        //ProductID has changed since the page was first loaded, so react accordingly. 
        //You can use the original ProductID from the first load, or reset it to match the one in the Session.
        //If you use the earlier one, you may or may not want to reset the one in Session to match.
    }
}

In the above code, note that changes to the ViewState (including the value of a Hidden control) will only take effect on the next PostBack. On a refresh, they'll revert to their most recent value. In my case, that was what I wanted, but it sounds like it's not quite right for your situation. Still, that information could be useful to you, depending on how you implement this.

I've left out a discussion of comparing currentProductID to Session[PageID.Value + "ProductID"], since I've already posted a lot of code, and I don't know the details of what you're trying to do. But there are various ways you can use Session, ViewState, and the QueryString to glean information about the state and history of the page.

Hopefully that should give you the general idea. Let me know if that's not enough to get you going.

like image 44
Justin Morgan Avatar answered Mar 07 '23 04:03

Justin Morgan