Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 2647 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > what is view state?give the realtime example. ...
Bageshkumarbagi

what is view state?give the realtime example.

Replies: 3 | Posted by: Bageshkumarbagi on 10/15/2012 | Category: ASP.NET Forums | Views: 705 | Status: [Member] | Points: 10  


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


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Dnandha87
Dnandha87  
Posted on: 10/15/2012 6:13:22 AM
Level: Starter | Status: [Member] | 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. | Reply | Alert Moderator 

Ranjeet_8
Ranjeet_8  
Posted on: 10/16/2012 5:26:43 AM
Level: Gold | Status: [Member] | 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. | Reply | Alert Moderator 

Satyapriyanayak
Satyapriyanayak  
Posted on: 1/24/2013 1:30:43 AM
Level: Silver | Status: [Member] | 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;
}

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

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/20/2013 8:35:52 AM