Vs IsPostBack Page Update

I have an index page that sends users to the product editing page in separate browser tabs.

For each edited product, the index overwrites the session ["ProductID"].

Then on the "Edit" page there is the following code for the unique identifier of 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 several tabs, I only refer to Session [PageID.Value + "ProductID"] in my code so that I always have the correct identifier. (I work in a reliable environment, this is for the intranet, so I'm not too concerned about the level of security).

My problem arises if the user refreshes the page by pressing F5. At this point, the [PageID.Value + "ProductID"] session receives the ["ProductID"] session of the last product that it opened.

For instance:

User 1 opens product1 in tab1

User 1 opens product2 in tab2

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

User 1 on page product1 gets to the update button (F5), page product1 becomes page product2

Is there a way to detect a page refresh from the “first load / redirect from another page” so that I can then tell my page that it is not updating my session [PageID.Value + "ProductID"]?

+4
source share
3 answers

I solved a very similar problem by saving two versions of the state-determining parameter: one in the session and one in the ViewState or URL (QueryString).

If you compare the two values ​​in Page_Load, this will tell you if the session variable has changed since the first page load. This should be exactly what you need.

EDIT: Rough code sketch (warning - did not see 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 code above, note that changes to the ViewState (including the value of the hidden control) will only take effect for the next PostBack. When updated, they will return to their last value. In my case, it was what I wanted, but it sounds like it is not quite right for your situation. However, this information may be useful to you, depending on how you implement it.

I have not discussed comparing currentProductID with Session[PageID.Value + "ProductID"] since I have already posted a lot of code and I don’t know the details of what you are trying to do. But there are various ways to use Session, ViewState, and QueryString to get information about the status and history of the page.

Hope this should give you a general idea. Let me know if this is not enough for you to go.

+2
source

Personally, I would go for the URL parameters. For instance. Pass product identifiers as URL parameters.

If you need pages without parameters, you can, for example,

  • Pass parameter to page.
  • The page reloads if the parameter is present and deletes the parameter

Thus, you can distance yourself from the first call (= parameter is present) and the second + call (parameter is absent).

+4
source

You can see this . I think this is close to what you are looking for.

+2
source

Source: https://habr.com/ru/post/1337051/


All Articles