How do I disable viewstate for a specific control?
<a开发者_运维问答sp:TextBox ID="TextBox1" runat="server" EnableViewState="false" />
<asp:Button ID="Button1" runat="server" Text="Button" />
I have set the EnableViewState property to false, but when I click on the button the value in the textbox persists after the postback. Why does the value persist?
Have a look at Understanding ASP.NET View State. In the page lifecycle, there is a Load Post Data stage that will populate your control values from the form data.
View State can be very confusing, specifically why you need it if controls are populated with form data on post back. The Role of View State from the same link above does a decent job of explaining why it's useful.
To summarize: View State is not required for user input. View State is used to store programmatic changes to a pages state that occur. A simple example is when a non-submit button is clicked and the handler alters a label's text. That change should be stored in the View State so it is persisted across additional post backs.
The controls that accept input can have their state restored by using the data posted to the server. They needn't be stored in the ViewState. In a way these are not the old values these are the NEW values that the user submitted (despite the fact that he may not have changed them).
Explanation
The simplest way is to set each time the Text property to String.Empty.
精彩评论