Click here to Skip to main content
15,881,616 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i want know why viewstate variable data updated on button click but not on page refresh in asp.net
Posted
Comments
Devang Vaja 3-Oct-12 5:47am    
not clear question please elaborate it
Vijay Walunj,Navi mumbai 3-Oct-12 6:00am    
i have one web page in this one button control i have added.i am for first time postback i am assing current date to one session variable.In pre render method i am asssigning it to viewstate variable.on button click i am matching this to variable value if it match i am changing session varible value to current time so now there is different time in session and viewstate variable.After buttonclick method executed prerender method is called in this viewstate varible value set to session variable value.if now i am click on submit button again i found that my viewstate and session varible values are same but in case of page refresh it not same.my code is

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Session("Time") = Date.Now.ToString
End If
End Sub

Private Sub Statemanagement_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
ViewState("Time") = Session("Time")
End Sub


Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click


If ViewState("Time") = Session("Time") Then
MsgBox("Match")
Session("Time") = Date.Now.ToString
End If
End sub

When you click the refresh button of the browser it is sending a GET query to your web server. Then ASP.Net do not receive any viewstate information from the browser. It is considered as a totally new Request.
When you click a button on your page then the browser send a POST query to your server, this one will include the viewstate information from the browser.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900