c# - Website doesn't see newest session values until I refresh page, but refreshing resets values of controls -
my asp.net website doesn't see newest session
values. when form beingness sent server after clicking button, result sent browser depends of session
values. process them in page_load
method. doesn't see lastly alter od session
values, 1 before last. looks button's event handlers executed before page's event handles i'm not sure if that. tried code in other methods page_preinit etc, it's still same that. thing works me refreshing page: response.redirect(request.url.absoluteuri);
after alter of session
value, resets values of controls, want same before. there improve solution?
example: when runs first time, label's text "click button", when click of buttons 1 time, nil happens. when click of button sec time, label's text value of first click (even if click , b, value changes after clicking b).
form:
<form id="form1" runat="server"> <div> <p> <asp:label id="label1" runat="server" /> </p> <p> <asp:button id="button1" runat="server" text="a" onclick="button1_click" /> <asp:button id="button2" runat="server" text="b" onclick="button2_click" /> </p> <p> <asp:textbox id="textbox1" runat="server"></asp:textbox> </p> </div> </form>
event handlers:
protected void page_load(object sender, eventargs e) { label1.text = session["letter"].tostring(); } protected void button1_click(object sender, eventargs e) { session["letter"] = "a"; } protected void button2_click(object sender, eventargs e) { session["letter"] = "b"; }
global method:
void session_start(object sender, eventargs e) { session["letter"] = "click button"; }
without code difficult, sounds setting session collection value in code of button's click event( or other control/event ), , expecting in session collection during page_load event.
it doesn't work - when page request comes in, page_load happens before control's events.
instead of page_load utilize page_prerender event occurs before page prepared sent client.
your add-on of code confirms above.
normally wouldn't utilize session_start initialize stuff this, utilize page_load , ispostback property
protected void page_load(object sender, eventargs e) { if(!ispostback){ label1.text = "click button"; } }
c# asp.net .net session
No comments:
Post a Comment