简体   繁体   English

如何关闭viewstate?

[英]How do I turn off viewstate for good?

Coming from a PHP background I love using clean URLs to grab data from one service to another. 来自PHP背景我喜欢使用干净的URL从一个服务获取数据到另一个服务。

However, on some of my ASP.NET projects I get the horrible ViewState parameter in my URLs. 但是,在我的一些ASP.NET项目中,我在URL中获得了可怕的ViewState参数。

Is there a way to turn this off globally? 有没有办法在全球范围内解决这个问题?

What affect will this have on my ASP.NET app? 这会对我的ASP.NET应用程序产生什么影响?

You can turn off viewstate for the whole site like this: 您可以像这样关闭整个网站的viewstate:

    <system.web>
<pages enableViewState="false" />

That said, you shouldn't be getting it on the url. 也就是说,你不应该把它放在网址上。 ViewState is a hidden field that is sent to the server with a postback (which normally uses post). ViewState是一个隐藏字段,通过回发(通常使用post)发送到服务器。 It keeps the state of the controls when the page was rendered to the client, sending it with each postback. 它在页面呈现给客户端时保持控件的状态,并在每次回发时发送它。 If it works for the application you could switch to use post instead (the problem form is surely using get), if not take a look at Jon's answer. 如果它适用于应用程序,你可以切换到使用post(问题表单肯定是使用get),如果不看看Jon的答案。

Check this link for more information on how the viewstate fits into the asp.net life cycle: http://msdn.microsoft.com/en-us/library/ms972976.aspx . 有关viewstate如何适应asp.net生命周期的更多信息,请查看此链接: http//msdn.microsoft.com/en-us/library/ms972976.aspx

I had a similar question when writing the Reputation Tracker . 在撰写信誉跟踪器时,我遇到了类似的问题

I don't know how you do it globally other than to just never use a form with runat="server" set, which is more to do with discipline than a setting. 我不知道你是如何在全球范围内做的,只是从不使用runat="server"设置的表单,这更多地与纪律而不是设置有关。 In particular, if you have runat="server" set on a form I believe you'll always get a viewstate parameter, even if you've turned it off everywhere so you don't get any values . 特别是,如果你在表单上设置了runat="server" ,我相信你总是会得到一个viewstate参数,即使你已经把它关掉,所以你没有得到任何 That was my experience, anyway. 无论如何,这是我的经验。

Obviously this limits you somewhat, but I've found that using the HTML server controls (instead of the ASP.NET controls) for appropriate parts of ASP.NET can make life a lot simpler to understand. 显然这对你有所限制,但我发现在ASP.NET的适当部分使用HTML服务器控件(而不是ASP.NET控件)可以让生活变得简单易懂。

Turn off the ViewState by default by using a <page> element in the web.config. 默认情况下,使用web.config中的<page>元素关闭ViewState。 Using EnableViewState="true" in the @Page directive will no longer work once you disable the ViewState in the web.config. 一旦在web.config中禁用ViewState,在@Page指令中使用EnableViewState="true"将不再有效。 If you decide later that you need the ViewState for a specific page, you can turn it back on for just that page using a <location> element. 如果您稍后决定需要特定页面的ViewState,则可以使用<location>元素将其重新打开。

<configuration>
  <system.web>
    <pages enableViewState="false" />
  </system.web>

  <location path="MyFolder/MyPage.aspx">
    <system.web>
      <pages enableViewState="true" />
    </system.web>
  </location>
  <location path="Site.master">
    <system.web>
      <pages enableViewState="true" />
    </system.web>
  </location>
</configuration>

You need to do the same for any master pages that your ViewState enabled page uses. 您需要对启用ViewState的页面使用的任何母版页执行相同操作。

将其添加到web.config文件:

<Pages enableViewState="false"/> 

You could switch to ASP.Net MVC. 您可以切换到ASP.Net MVC。 From what I understand it doesn't use the ViewState. 据我所知,它不使用ViewState。

Do recall, however, that certain behaviors expected by most ASP.NET web forms developers will not work without ViewState. 但请记住,如果没有ViewState,大多数ASP.NET Web表单开发人员所期望的某些行为将无法运行。 The purpose of ViewState is to provide the illusion that various page and control properties persist from one request to the next. ViewState的目的是提供各种页面和控件属性从一个请求持续到下一个请求的错觉。 ViewState does not contain all control properties, just those that have changed. ViewState不包含所有控件属性,只包含已更改的控件属性。 The idea is that ViewState retains these properties as they were at the time the form last rendered. 我们的想法是ViewState保留了上次呈现表单时的属性。

One good example is a SelectedIndexChanged event on a dropdown (one that does not have autopostback set). 一个很好的例子是下拉列表上的SelectedIndexChanged事件(没有设置autopostback的事件)。 This works because ViewState retains the previous index, and the form posts the current index, and the control compares the two in order to know that the selected index has changed. 这是因为ViewState保留了先前的索引,并且表单发布了当前索引,并且控件将两者进行比较以便知道所选索引已更改。 That's when it raises the SelectedIndexChanged event. 那是它引发SelectedIndexChanged事件的时候。 Without ViewState, that event will not fire. 没有ViewState,该事件将不会触发。 Same for TextChanged events, etc. 对于TextChanged事件等也是如此

Absent the GET situation (which I have never run into), the big problem with ViewState is using it where it's not needed. 如果没有GET情况(我从未遇到过),ViewState的一个大问题是在不需要的地方使用它。 Your grid control does not need to retain the previous values of all controls in all its rows, so don't enable ViewState on it. 您的网格控件不需要在其所有行中保留所有控件的先前值,因此不要在其上启用ViewState。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM