How do I disable viewstate for a specific control?

2019-02-20 11:25发布

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

3条回答
疯言疯语
2楼-- · 2019-02-20 11:49

Explanation

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-02-20 11:56

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.

查看更多
何必那么认真
4楼-- · 2019-02-20 12:11

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

查看更多
登录 后发表回答