I am confused in how to use ViewState in C#, for example what is the benefit of using:
ViewState["VST"]="Value1";
Lable1.Text= ViewState["VST"].ToString();
Whereas I can use:
string container= "Value1";
Lable1.Text= container;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your ViewState consists of variables that are kept with the post-backs of your page, because they are sent to the client and the client sends them back with the entire page.
Hence, if you do:
Then the users sees the page and hits the submit button, your
containerstring will not exist.If however you uses the ViewState, ViewState[“VST”] will still have the value as it will be “refreshed” when the user submits and sends the page back.
Read more here and also understand the ASP.NET page life cycle.