what is the use of viewstate in asp.net

Posted by Shanky11 under ASP.NET on 12/14/2012 | Points: 10 | Views : 1761 | Status : [Member] | Replies : 8
viewstate in asp.net ????????/
if we write view state enabled="true" or"false" what's happen..............at @page directive?????????nothing is happening then why it is used
please provide the practicall details of viewstate
what's the diff b/w session and viewstate???????????????????




Responses

Posted by: Pavanandey on: 12/14/2012 [Member] Bronze | Points: 25

Up
0
Down
http://msdn.microsoft.com/en-us/library/ms972976.aspx
http://msdn.microsoft.com/en-us/magazine/cc188774.aspx

These are the differences between Session & Viewstate in .Net web applictions:
1) Viewstate is particular to a control/page whereas session is for the whole application. You can access the session data stored in page A in page B but viewstate does not behave so.
2) ViewState is maintained at client side whereas session is maintained at server side
3) Since viewstate is maintained at client, it does not have a concept of expiry but session will expire after the stipulated time
4) You can enable/disable view state for specific controls but you can't do that for session.
5)Viewstate will be stored as encrypted value in the client page itself, where as session will not be stored anywhere other than memory.
6)Since session uses memory more and if you wants to load lot of data in session you will be adding extra load on server.
7)Eventhough viewstate values are encrypted still that can be hacked
8)We cant compare this two both has pro and cons so based on the scenario we have to use session or viewstate.
9) One more thing "Cache" is there which will also stores data but that is not page specific or user specific it is a general one of the head ache is we need to clean up the cache properly.
10)if we doesnt need the viewstate in a particular page we can disable that at page level so page will render very fast.



Thanks
Pavan Kumar
Mark Answer if this fits the need

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

Posted by: Shanky11 on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
then what is the use of viewstate["vwstate"]=textbox1.text;
what is the use of this implementation???????

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

Posted by: Pavanandey on: 12/14/2012 [Member] Bronze | Points: 25

Up
0
Down
you can save the same textbox1.text value either in the session or viewstate which can be accessed in other pages in the application

Thanks
Pavan Kumar
Mark Answer if this fits the need

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

Posted by: Dotnetrajanikanth on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try This One,

Create a web application project.

Select a page.

Add One Label and 2 Buttons.

In the label give the text as "Initial Text".

Then In one of the button write the code for changing the text of the label.

LblInitial.Text = "New Text"

Then run the application.

Click the button which you have written code.

you can see that the text has been changed from initial to New Text.

Now click the other button. which has no code. what you are seeing? now you wont notice any change.

Now change the view state of label to EnableViewState = 'false'

then click the button to change the text of the label. and after changing click the other button.

now after the post back you can see that the label will come to the initial state.

this is because of view state.

View state is just to preserve the state of the page or control during the postbacks.

____________
www.flickr.com/photos/psdesigner/

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

Posted by: Shanky11 on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
viewstate is accessible in particular page not like session that is accessible from all pages

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

Posted by: Sandhyab on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
ViewState is a way to store temporary data on the page that can be retrieved in the post back.

Thanks & Regards

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

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

Up
0
Down
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.





Ex:-

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

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

Posted by: Chaithragm on: 1/24/2013 [Member] Starter | Points: 25

Up
0
Down
im using file upload control to uploading of resume and documents in the registration page after clicking the terms and conditions(i have used chechbox)post back is happening and no has been uploaded to server , How to use view state here? to retrive the data after the postback?

//this is the code i have used before using viewstate
<asp:FileUpload ID="FileUpload1" runat="server" />(i have enabeled the view state for this control)

byte[] upload_resume ;
byte[] upload_certificate ;

upload_resume = FileUpload1.FileBytes;
upload_certificate = FileUpload2.FileBytes;

//after using viewstate

ViewState["resume"] = FileUpload1.FileBytes;


byte[] city = (byte[])ViewState["resume"];
upload_resume = resume;
upload_resume = FileUpload1.FileBytes;



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

Login to post response