I use viewstate like this:
public bool SearchClicked
{
get { return Session["bool"]==null? false : (bool)Session["Bool"]; }
set { Session["bool"] = value; }
}
In The code whenever the button pressed, the events triggered.
I set it:
SearchClicked=true;
With every post back I check if it is true or not:
if (SearchClicked)
{
}
When I start the site and there is a postback, the value is set to “true”.
It is true that i pressed the button that set it to true a few times before when i run the application. But the application instance is new everytime I run the application. Doesnt it mean that the ViewState resets itself with every time I run the application and doesnt save its state like Session (20mins)?
In your code above, you are not using ViewState to store the SearchClicked value, but rather you are using the Session to store it. ViewState is persisted on the page in a hidden value and gets posted back to the server. This means that the viewstate is stored on the browser, and therefor will persist as long as the user is on the page. Session information is stored on the server. A users session will eventually time out, but other things can cause the session to reset, such as rebuilding the application or editing the web.config file.
To store to ViewState use the following