c# - Page refresh Vs IsPostBack -
i've got index page sends users edit products page on separate browser tabs.
for each products edited index rewrites session["productid"].
the edit page has following code have unique identifier tab , 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, , when same user opens multiple tabs reference session[pageid.value + "productid"] in code have proper id. (i'm working in trusted environment intranet, therefore i'm not bothered level of security).
my issue occurs if user page refresh hitting f5 key. @ point session[pageid.value + "productid"] gets session["productid"] of last product opened.
for example:
user 1 opens product1 in tab1
user 1 opens product2 in tab2
whenever use tool normally, works fine. if:
user 1 on product1 page hits refresh button (f5) product1 page becomes product2 page
is there way detect page refresh "first load/redirect page" can tell page not update session[pageid.value + "productid"]?
i've solved similar issue storing 2 versions of state-identifying parameter: 1 in session , 1 in either viewstate or url (querystring).
if compare 2 values on page_load, tell whether session variable has changed since page first loaded. should need.
edit: rough sketch of code (warning - haven't seen actual code since wrote 3 years ago):
protected string currentproductid { { 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 problem occurs when not posting back, wrap below in // if(!ispostback) block. past issue occurred on both postbacks // , page refreshes. //note: i'm assuming session["productid"] should never null. if (currentproductid == null) { //loading page first time. currentproductid = (string)session["productid"]; } else if (currentproductid != session["productid"]) { //productid has changed since page first loaded, react accordingly. //you can use original productid first load, or reset match 1 in session. //if use earlier one, may or may not want reset 1 in session match. } }
in above code, note changes viewstate (including value of hidden control) take effect on next postback. on refresh, they'll revert recent value. in case, wanted, sounds it's not quite right situation. still, information useful you, depending on how implement this.
i've left out discussion of comparing currentproductid
session[pageid.value + "productid"]
, since i've posted lot of code, , don't know details of you're trying do. there various ways can use session, viewstate, , querystring glean information state , history of page.
hopefully should give general idea. let me know if that's not enough going.
Comments
Post a Comment