I have one issue for ViewState

浪子不回头ぞ 提交于 2019-11-30 09:39:11

问题


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


回答1:


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




回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/16064621/i-have-one-issue-for-viewstate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!