Is it possible to disable ViewState in ASP .NET?

2019-08-28 11:30发布

问题:

I am using third party server-side controls on my website (Telerik RadControls for ASP .NET). The menu control has a complex structure which is bloating the size of the viewstate affecting load times. I am not aware of the details of viewstate except that it is used by the web server to keep track of control values.

Since most of the pages on my website are display only, is it possible to disable the viewstate where user input is not taken? All dynamic items are being regenerated on page load in any case.

回答1:

If you're using asp.net 4, then you can use the ViewStateMode property to control viewstate generation for controls. ViewStateMode

If you're not using asp.net 4, then you can turn of viewstate at the page or control level.

<%@ Page Language="C#" EnableViewState="false"%> //disabling viewstate. 

http://msdn.microsoft.com/en-us/library/system.web.ui.page.enableviewstate.aspx



回答2:

In the top of your aspx page add the parameter enableViewstate="false" on the pages that doesn't need it. it won't disable the viewstate completely though, but reduce the size seriously

I haven't tested this, but I think it's possible..

In your Web.config file add the attribute to the pages tag instead, and then on those pages you need it enabled, add the attribute with true in the top



回答3:

System.Web.UI.WebControls.CheckBox checkBox = new System.Web.UI.WebControls.CheckBox();
checkBox.EnableViewState = false;


回答4:

There are different ways of disabling ViewState. In your case, I would recommend disabling the viewstate on the control:

<telerik:RadMenu ViewStateMode="Disabled"