Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable ASP.NET viewstate for some controls but not all

Tags:

asp.net

How can I disable viewstate in my ASP.NET page for most controls, but allow some controls to still use ViewState?

I have tried:

  • In the properties I change EnableViewState=false
  • Use <%@ Page Language="C#" EnableViewState="false" ... > at the top of the page

But how do I enable some controls to still allow viewstate?

I am using .NET 4.

like image 537
Mahdi jokar Avatar asked Dec 13 '22 05:12

Mahdi jokar


2 Answers

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>
like image 84
Mahdi jokar Avatar answered Dec 14 '22 18:12

Mahdi jokar


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.

like image 39
Jon Adams Avatar answered Dec 14 '22 18:12

Jon Adams