sly_Chandan
sly_Chandan

Reputation: 3515

How do I disable viewstate for a specific control?

<asp: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?

Upvotes: 2

Views: 4264

Answers (3)

Stilgar
Stilgar

Reputation: 23551

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).

Upvotes: 0

Mufaka
Mufaka

Reputation: 3444

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.

Upvotes: 3

zavaz
zavaz

Reputation: 745

Explanation

The simplest way is to set each time the Text property to String.Empty.

Upvotes: -2

Related Questions