Hi,
What is the ViewState in Asp.Net?
Plz give me an example.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Oct 19, 2012, 8:06 AM
The view state is control it will be used to maintain the state of the control across the posted back to the server. The value will be stored in the hidden control. Every time it needs to encryption and decryption (serialize and deserialize across the post backs).
The view state can be enabled by setting the property called EnableViewState="true/false". It can be set in the machine config/ web config/ page directives/ control level.
By default it is false. You can enable it by setting boolean flag.
This can be used with in the page and not across the page.
Viewstate["Version"] = txtVersionName.Text.ToString();
The view state also can store the data using the key and value combination. The data will be serialized and stored in the hidden control You can view the source code page and find the hidden control with _viewstate.
Drawbacks:
- Increase the page payload (when you have grid with many rows then every thing has to be loaded and retain again with serialize and deserilize)
- Additional overhead while serialize and deserialize.
- Increase the memory allocation on the server.
Please refer the below link
http://msdn.microsoft.com/en-us/library/ms972976.aspx
protected void Button1_Click(object sender, System.EventArgs e)
{
ViewState["FavoriteColor"] = TextBox1.Text.ToString();
ViewState["City"] = TextBox2.Text.ToString();
Label1.Text = "Your data saved in ViewState.";
}
protected void Button2_Click(object sender, System.EventArgs e)
{
string color = (string)ViewState["FavoriteColor"];
string city = (string)ViewState["City"];
Label1.Text = "Hi your favorite color is: " + color +
"
and you came from: " + city;
}
Shivanand ArurPosted Oct 19, 2012, 8:18 AM
http://www.c-sharpcorner.com/UploadFile/78d182/Asp-Net-state-management-techniques/
Thank you,
Please mark the answer as Accepted if it helped you!!!
R ACHUTHAPosted Oct 19, 2012, 7:05 AM
refer the below url
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-State