Search
Sponsor
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Forums Author
Thu, 02-Oct-2014 Authors
All Time Authors
Jayakumars
4170
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)
 Forum Post

what is view state?give the realtime example.

Bageshkumarbagi
Posted by Bageshkumarbagi under ASP.NET on 10/15/2012 5:58:58 AM | Points: 10 | Views : 10519 | Status : [Member] | Replies : 3


hi,
plz give me the real time example of view state .whrer we use view state?

Thanks,
Bagesh Kumar Singh
Mark as answer if you have been satisfied


Posted by: Dnandha87 on: 10/15/2012 [Member] Starter | Points: 25

View State:

Viewstate is used to store data that are accessed by the same page. Ideally a POSTBACK situation.
For example, think of a report. The filter condition is appended as and when the user selects a condition and finally when the user click submit button, the report is generated. Here we can use viewstate to save the filter condition since the same page is posted back. When we use session here, the filter condition is lost on session expiry.

In general, its a good practice to disable viewstate for datagrids for lengthy reports.

Regards
Nandha Kumar,

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 10/16/2012 [Member] Gold | Points: 25

Viewstate object is used to persist data of variables across postbacks. In ASP.NET, a variable's value is assigned to a viewstate object and then this is passed as a hidden variable and then may be retrieved by a page after a postback. See the example below...

//Save the value in ViewState object before the PostBack
ViewState["SomeVar"] = txtFirstName.text;


//Retrieve the value from ViewState object after the PostBack
String strFirstName = ViewState["SomeVar"].ToString();

Note that Viewstate object's value is accessible only at page level. This means that if a viewstate is created at page1.aspx, then it may be used only within page1.aspx after the postback, and cannot be used by any other page.


Refer this url
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-State


.

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/24/2013 [Member] Silver | Points: 25

The View State is client state mechanism in the ASP.Net statement management.

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.



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 +
"<br />and you came from: " + city;
}

If this post helps you mark it as answer
Thanks

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response


Found interesting? Add this to: