Is it possible to disable ViewState in ASP .NET?

2019-08-28 10:50发布

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.

4条回答
别忘想泡老子
2楼-- · 2019-08-28 11:30

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楼-- · 2019-08-28 11:31
System.Web.UI.WebControls.CheckBox checkBox = new System.Web.UI.WebControls.CheckBox();
checkBox.EnableViewState = false;
查看更多
地球回转人心会变
4楼-- · 2019-08-28 11:47

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

<telerik:RadMenu ViewStateMode="Disabled"
查看更多
Luminary・发光体
5楼-- · 2019-08-28 11:51

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

查看更多
登录 后发表回答