what is the use of viewstate in asp.net

Posted by Shanky11 under ASP.NET on 12/14/2012 | Points: 10 | Views : 2793 | Status : [Member] | Replies : 7
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: 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