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"
?
Texbox Doesnt Use Viewstate here is the link to explain all Link Explain
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:
then the color you set will be preserved across postbacks in ViewState, if it is enabled.
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
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".