简体   繁体   English

禁用某些控件的ASP.NET视图状态,但不是全部

[英]Disable ASP.NET viewstate for some controls but not all

How can I disable viewstate in my ASP.NET page for most controls, but allow some controls to still use ViewState? 如何在ASP.NET页面中禁用大多数控件的viewstate,但允许某些控件仍使用ViewState?

I have tried: 我努力了:

  • In the properties I change EnableViewState=false 在属性中,我更改了EnableViewState=false
  • Use <%@ Page Language="C#" EnableViewState="false" ... > at the top of the page 在页面顶部使用<%@ Page Language="C#" EnableViewState="false" ... >

But how do I enable some controls to still allow viewstate? 但是如何启用某些控件仍然允许viewstate?

I am using .NET 4. 我使用的是.NET 4。

I found a way, but only use this in the .NET framework 4. Put this in page directive: 我找到了一种方法,但只在.NET框架中使用它4.将它放在页面指令中:

<% ----    ViewStateMode="Disabled" %>

and use this for each controls that want to save the Viewstate (For example): 并将其用于要保存Viewstate的每个控件(例如):

<asp:DropDownList ID="comGrade" runat="server" ViewStateMode="Enabled">
</asp:DropDownList>

ASP.NET 4 allows for more control over the viewstate. ASP.NET 4允许更多地控制视图状态。 See MSDN's documention on the ViewStateMode property. 请参阅MSDN关于ViewStateMode属性的文档。 Also the question on SO Minimizing viewstate- confused by EnableViewState and ViewStateMode in asp.net 4.0 . 另外关于SO 最小化视图状态的问题 - 在asp.net 4.0中由EnableViewStateViewStateMode混淆

In ASP.NET prior to v4, disabling ViewState disables it for all children as well, regardless of setting a child to EnableViewState="true" . 在v4之前的ASP.NET中,禁用ViewState也会禁用所有子项,无论将子项设置为EnableViewState="true" In ASP.NET 4 you may re-enable the child by following the MSDN docs suggestion: 在ASP.NET 4中,您可以按照MSDN文档建议重新启用子项:

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 . 要为页面禁用视图状态,并使其能够在页面上特定的控制,设置页面的EnableViewState属性和控制为true,设置页面禁用ViewStateMode属性,并设置控件的ViewStateMode属性启用

Sounds like a job for ControlState: 听起来像ControlState的工作:

http://msdn.microsoft.com/en-us/library/1whwt1k7.aspx http://msdn.microsoft.com/en-us/library/1whwt1k7.aspx

Set the ViewStateMode to Disabled on the page, and omit EnableViewState . 在页面上将ViewStateMode设置为Disabled ,并省略EnableViewState You can set it to enabled for some controls (default is Inherit ). 您可以将其设置为某些控件的启用(默认为Inherit )。

Edit (sidenote, see comments too): 编辑(旁注,也见评论):

As we discussed in the comment, text boxes keep their value even though ViewState is disabled. 正如我们在评论中所讨论的那样,即使ViewState被禁用,文本框也会保留其值。 This is true, as they are elements of a HTTP POST request. 这是事实,因为它们是HTTP POST请求的元素。 Labels aren't, for instance, as they render to span tags. 例如,标签不会渲染为span标签。

In the following sample code, there is a label filled with a random number. 在以下示例代码中,有一个标有随机数的标签。 If you set ViewStateMode to Enabled , the random number from the last request is written to the Debug Output Window. 如果将ViewStateMode设置为Enabled ,则将上次请求中的随机数写入Debug Output Window。 If you set ViewStateMode to Disabled , an empty string is written. 如果将ViewStateMode设置为Disabled ,则会写入空字符串。 The Label 's state is not kept: 不保留Label的状态:

<%@ Page Language="C#"
    AutoEventWireup="true"
    CodeBehind="Default.aspx.cs"
    Inherits="WebApplication2.Default"
    ViewStateMode="Disabled" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:TextBox runat="server" ID="textbox" Text="Default" />
            <asp:Label runat="server" ID="randomNumberLabel" />
            <asp:Button runat="server" Text="Click Me" />
        </form>
    </body>
</html>

This is the code behind. 这是背后的代码。 Be sure to attach a debugger: 请务必附加调试器:

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        private readonly static Random rnd = new Random();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(this.IsPostBack)
                Debug.WriteLine(this.randomNumberLabel.Text);

            this.randomNumberLabel.Text = rnd.Next(Int32.MaxValue).ToString();
        }
    }
}

I hope I managed to clarify the difference. 我希望我能澄清其中的不同之处。

ViewState can be disabled at the application, page, or control level. 可以在应用程序,页面或控件级别禁用ViewState。

To disable at the application level, put the following into your web.config file: 要在应用程序级别禁用,请将以下内容放入web.config文件中:

<Pages EnableViewState="false" ... />

To disable a particular page, you do it declaratively in the page directive: 要禁用特定页面,请在页面指令中以声明方式执行此操作:

<%@ Page EnableViewState=”false”

or programmatically in a page event: 或以编程方式在页面事件中:

private void Page_Init(object sender, System.EventArgs e)
{
    this.EnableViewState = false;
}

Finally, to disable viewstate on a particular control, you can use the following: 最后,要禁用特定控件上的viewstate,可以使用以下命令:

<asp:datagrid EnableViewState="false" ... />

Keep in mind that certain controls will not work properly with viewstate turned off. 请记住,某些控件在关闭视图状态时无法正常工作。 Sometimes you can keep viewstate enabled for a particular control but minimize the size of the viewstate payload by carefully determining where in the ASP.Net eventing pipeline to populate the control. 有时您可以为特定控件启用viewstate,但通过仔细确定ASP.Net事件管道中填充控件的位置来最小化视图状态有效负载的大小。 You can read this excellent reference for more information. 您可以阅读此优秀参考资料以获取更多信息。

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

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