c# - Website doesn't see newest session values until I refresh page, but refreshing resets values of controls -


My asp.net website does not see the latest session value when clicking on the server If my form is being sent, then the browser sent back to the result is based on the session values. I'll process them in the Page_load method. This last change od Session does not see value, but before the last one. It seems that the event handlers of the buttons are executed before the event on the page, but I'm not sure it is the same. I tried to code my code in other ways like Page_ PreInit, but it is still similar. The only thing that works for me is that the page refreshes: feedback. Redirect (request. URLSOLUTURI); After any change of any session value, but it resets the values ​​all the controls, which I want to be as before. Is there a better solution?

Example: When it first runs, the text of the label is "click on the button", but when I click on a single button, nothing happens go. When I click any button for the second time, the text of the label is the value of the first click (even if I click on A and then B changes to click on value B).

Form:

  & lt; Form id = "form1" runat = "server" & gt; & Lt; Div & gt; & Lt; P & gt; & Lt; Asp: Label ID = "Label1" runat = "server" / & gt; & Lt; / P & gt; & Lt; P & gt; & Lt; ASP: Button ID = "Button 1" Runat = "Server" Text = "A" OnClick = "Button 1_Click" /> and & nbsp; & Lt; ASP: Button ID = "Button 2" Runat = "Server" Text = "B" OnClick = "Button 2_Click" /> & Lt; / P & gt; & Lt; P & gt; & Lt; ASP: Text Box ID = "Text Box 1" Runat = "Server" & gt; & Lt; / Asp: text box & gt; & Lt; / P & gt; & Lt; / Div & gt; & Lt; / Form & gt;   

Event Handler:

  Protected Zero Page_load (Object Sender, EventArgges E) {Label1.Text = Sessions ["Alphabets"]. ToString (); } Secure Zero Button 1_Click (Object Sender, EventArgs E) {Session ["Letter"] = "A"; } Secure Zero Button 2_Click (Object Sender, EventAGRS E) {Session ["Letter"] = "B"; }   

Global method:

  zero session_start (object sender, eventarges e) {session ["letter"] = "click on button"; }    

Without its code it is very difficult, but it seems that you Setting up session is expecting collection values ​​in the code of a button's click event (or some other controls / events), and session_ collection during the page_load event.

It does not work like this - When the page arrives in the request, there is Page_Load before the events of control.

Use Page_load instead of Page_PreRender this event is prepared to return the customer back to the page.

The code confirms the above.

Generally I will not use session_start to initialize the stuff like this, use Page_load and Ipostback property

  secure zero Page_load (object sender, EventArgs) {if (IsPostBack) {Label1.Text = "Click on the button"; }}    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -