Going nuts with Viewstate

S

sourabh

Hi guys
Till now i thougt I understood ViewState but as I was trying write my
LoadViewState method i am sort of confused with what this ViewState is and
what is its use.
I have my LoadViewState method , i was curious to know wheres the state of
Various controls getting set on Post back. So I tried viewing the ViewState
but it doesnot show any controls and their values. I also had to implement
SaveViewstate to have my LoadViewState get called and again this was called
only when my page added some values to the ViewState in SaveViewState
method.
So the confussion is on postback how are the values for each control get set
back to the values which i have set earlier.
protected override void OnLoad( EventArgs e ){

// Put user code to initialize the page here

if(!IsPostBack)

{

TextBox1.Text = "wEQ";

}

}



Here I am checking for IsPostBack so I am not setting them again. I thought
this cam for ViewState but cant see these there.

Or am i completely wrong here and ViewState doesnot do all this?





Thanks

Sourabh
 
S

Scott Mitchell [MVP]

So the confussion is on postback how are the values for each control get set
back to the values which i have set earlier.

For TextBoxes and DropDownLists, the values posted back by the page are
NOT stored in view state*. This is because the values are posted back
from the browser (the value attributes of the <input> elements for the
textboxes, and the value attribute from the selected <option> element in
the <select> element for DropDownLists).

What you might want to do is grab a copy of Reflector
[http://www.aisto.com/roeder/dotnet/] and check out how the TextBox and
other Web controls implement LoadViewState. Also, a highly recommended,
must-read book for control development is Developing Microsoft ASP.NET
Server Controls and Components
[http://www.4GuysFromRolla.com/ASPScripts/Goto.asp?ID=170]

Lastly, I have recently authored an article on view state for MSDN's
ASP.NET Dev Center. The article has yet to be published, but if you
shoot me an email I'd be happy to send you a draft version of the
article. It might help clear up some confusion.

Hope this helps, and happy programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top