I have one issue for ViewState

2019-01-28 19:22发布

What is the meaning of EnableViewState="false" and EnableViewState="true"?

I Know EnableViewState="false" = turn Off the ViewState and also EnableViewState="true" = turn On the ViewState

But What is the difference between EnableViewState="false" and EnableViewState="true" ?

I tried this code:

<form runat="server">
<asp:TextBox ID="TextBox1" EnableViewState="true" runat="server">
</asp:TextBox><asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>

I am really confused. When I used EnableViewState="true", I entered some values in textbox and click my button .Now the value is here in the textbox . Its same process when i set EnableViewState="false".

So What happens when EnableViewState="true" and EnableViewState="false" ?

4条回答
狗以群分
2楼-- · 2019-01-28 19:35

Texbox Doesnt Use Viewstate here is the link to explain all Link Explain

查看更多
对你真心纯属浪费
3楼-- · 2019-01-28 19:41

ViewState is used to persist properties of a control that are set server-side.

So, to take a contrived example, if you do something like the following in Page_Load:

if (!IsPostBack)
{
    TextBox1.ForeColor = ...;
}

then the color you set will be preserved across postbacks in ViewState, if it is enabled.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-28 19:44

Not all controls are affected by view state. The controls that implement IEventHandler or IDataHandler will not get affected on page postback if view state is disabled. Textbox is one such control. If you want to see the effect in your code. Try setting the label value at run time on postback like on Click of button and check the results

查看更多
狗以群分
5楼-- · 2019-01-28 19:47

Generally you should use EnableViewState="false" on all controls on the asp.net page. The viewstate of a control is most commonly needed when you want to preserve some visual appearance of the control itself. E.g. if you change the background color of control and you want to persist that across postbacks use EnableViewState="true".

查看更多
登录 后发表回答