3

Put a textbox, a checkbox and a button on a website. Set the "EnableViewState" property of textbox and checkbox to false. Write something into textbox and check the checkbox. Click the button.

Why is the textbox still written and the checkbox checked after response?

1
  • Ahmet, make sure to accept an answer if it solves your question, this foes for every question you ask. You can do this by clicking the checkmark next to the answer that helps, it gives you rep, the answerer rep, makes the answer easy to find for the next guy, and makes your questions much more desirable to answer, meaning you get answers quicker. Apr 23, 2010 at 23:20

2 Answers 2

8

Some things aren't totally dependent on ViewState. In the controls you listed, those values are available in the POST sent to the server, so they're gotten out of there and the controls restore their state that way.

Other things, like the text in a <asp:Label> for instance aren't sent back in any way, and they'll lose their data without ViewState. The same is true for other properties, like the styling of the textbox, etc...only it's value will be restored, because that's all that's sent back and as a result, all it's coded to grab and restore. If you were to say make it red, that would be lost on postback.

As a general rule, what a control can restore strictly from posted data will be restored on postback, everything else is lost.

-2

Because HTML Controls are Stateless control. Therefore Microsoft provide a feature of ViewState that help when a user sends the data into server or after post back the value remain same. Therefore you have to set the property "EnableViewState" to True. By default, all the ASP.NET controls have their EnableViewState set to True

1
  • I already know them. That's why I said "Set the "EnableViewState" property of textbox and checkbox to false." Apr 23, 2010 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.