How can I disable viewstate in my ASP.NET page for most controls, but allow some controls to still use ViewState?
I have tried:
EnableViewState=false
<%@ Page Language="C#" EnableViewState="false" ... >
at the top of the pageBut how do I enable some controls to still allow viewstate?
I am using .NET 4.
I found a way, but only use this in the .NET framework 4. Put this in page directive:
<% ---- ViewStateMode="Disabled" %>
and use this for each controls that want to save the Viewstate (For example):
<asp:DropDownList ID="comGrade" runat="server" ViewStateMode="Enabled">
</asp:DropDownList>
ASP.NET 4 allows for more control over the viewstate. See MSDN's documention on the ViewStateMode property. Also the question on SO Minimizing viewstate- confused by EnableViewState
and ViewStateMode
in asp.net 4.0.
In ASP.NET prior to v4, disabling ViewState disables it for all children as well, regardless of setting a child to EnableViewState="true"
. In ASP.NET 4 you may re-enable the child by following the MSDN docs suggestion:
To disable view state for a page and to enable it for a specific control on the page, set the EnableViewState property of the page and the control to true, set the ViewStateMode property of the page to Disabled, and set the ViewStateMode property of the control to Enabled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With