What does EnableViewState on a HyperLink do or mean?
<asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink>
What does it mean? and what will it do if I set it to true. Thanks! I looked it up, but the definition was not in simple terms.
ViewState is used to persist the state of control properties across postbacks. Disabling it means that any properties you set programmatically (in code-behind) won’t be persisted across page postbacks. However, if you declare all the values declaratively (in your .aspx page) then disabling it won’t make any difference.
A quick example:
Say you have this aspx mark-up with ViewState enabled:
And you do this in code-behind:
Even though you only set the ForeColor of the HyperLink to the colour red on the first load the HyperLink will still remain red after clicking the Button that performs the postback. That is because ViewState stores the value of the HyperLinks properties and re-creates them after postback.
If you try the exact same thing but with ViewState disabled on the HyperLink, when you click the submit button the HyperLink will revert back to its original colour. That is because viewstate isn’t “storing” the fact that you set it to be red.
In practical terms you can normally disable ViewState if:
A) Your page doesn’t perform any postbacks
B) You set all the properties declaratively
If you really want to understand ViewState I’d recommend reading TRULY Understanding ViewState.