Maintaining session state in ASP.NET 3.5 using C# with boolean -


i have boolean variables declared in class of program , set false. within method, change value of boolean variable true have refresh page clicking button or two. once asp.net page refreshes, value returns false when want true. how can prevent changing boolean variable original value , keep true. c# syntax this. example, have following:

public partial class _default : system.web.ui.page {     bool plus = false;     bool minus = false;     bool multiply = false;     bool divide = false;     bool equal = false; } 

down further in program have:

protected void btnmultiply_click(object sender, eventargs e) {     if (textbox1.text == "")     {         return;     }     else     {         multiply = true;         session["tag"] = textbox1.text;         textbox1.text = "";     } } 

after press above "multiply" button next press equals button below , page refreshes changing value of boolean multiply false:

protected void btnequals_click(object sender, eventargs e) {     equal = true;      if (plus)         {              decimal dec = convert.todecimal(session["tag"]) + convert.todecimal(textbox1.text);         textbox1.text = dec.tostring();     }         else if (minus)     {             decimal dec = convert.todecimal(session["tag"]) - convert.todecimal(textbox1.text);             textbox1.text = dec.tostring();         }         else if (multiply)     {             decimal dec = convert.todecimal(session["tag"]) * convert.todecimal(textbox1.text);             textbox1.text = dec.tostring();         }         else if (divide)         {             decimal dec = convert.todecimal(session["tag"]) / convert.todecimal(textbox1.text);             textbox1.text = dec.tostring();         }         return;     } 

so when evaluates (multiply), skips because reset false when supposed true , evaluate expression. main issue how keep variable true instead of reverting false , skipping (multiply)?

this due stateless nature of asp.net.

  1. when page first loads, instance of _default created , multiply initialized false. page rendered browser , reference _default instance lost.

  2. then user clicks multiply button. causes postback server. server creates new instance of _default , multiply initialized false. btnmultiply_click happens , sets multiply true. once again, page rendered browser , reference _default instance lost.

  3. then user clicks multiply button. causes postback server. server creates new instance of _default , multiply initialized false. btnequals_click happens , multiply still false.

one of ways asp.net maintains state between requests through session object. it's kept outside of _default , lives between requests.

instead of saving state in member variables in page, should store in session well. session["action"] = "multiply" , check during btnequals_click if( session["action"] == "multiply".

personally, use viewstate instead of session this. difference viewstate lives on single page , postbacks, while session exists across pages. you'd use similar syntax: viewstate["action"] instead of session["action"]. viewstate works sending data browser in hidden field in markup, resubmitted server on postback.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -