Just a sanity check : ViewState vs PostBackData

Surprisingly, I have come across quite a few ASP.NET developers who misjudge the exact concept of a ViewState. So here is a really simple and stupid scenario. You have the following things on a ASP.NET page –:

- ASP.NET Textbox

- ASP.NET Button (which just does a postback)

So you load the page, enter some value in the textbox and click the  Button. Of course, that value in the textbox persists after postback. Now disable the ViewState of the textbox. Enter some value in the textbox again and click the Button. I have heard many folks confidently saying that there would be no text in that textbox, which is freggggginnn WRONG! The text will be there.

Explanation –:

ViewState has nothing to do with what you enter in an ASP.NET page while it is running. ViewState stores only the following 2 things –:

- Programmatic change to the properties of a control

- Dictionary storage for any value to be persisted across postbacks i.e. in ViewState[“key”].

The stuff entered in a textbox is persisted due to something called as the LoadPostBackData event during a Page life cycle. As a matter of fact, the data entered inside that textbox is stored within that page like the ViewState but is not a part of the ViewState. During the LoadPostBackData event, this value is picked up and assigned to the textbox.

If I am sounding nuts, please visit this detailed and point accurate explanation here http://www.codeproject.com/KB/aspnet/aspnetviewstatepagecycle.aspx.


Post a Comment