4

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.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Mahdi jokar
  • 1,267
  • 6
  • 30
  • 50

5 Answers5

7

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>
Richard Pursehouse
  • 1,109
  • 13
  • 21
Mahdi jokar
  • 1,267
  • 6
  • 30
  • 50
4

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.

Community
  • 1
  • 1
Jon Adams
  • 24,464
  • 18
  • 82
  • 120
  • when i set the ViewStateMode property of the page to Disabled. i cant set the ViewStateMode property of the control to Enabled. when i set the ViewStateMode property of the control to Enabled my control has not value after page postback – Mahdi jokar Feb 20 '12 at 18:59
  • @Mahdijokar: Did you _also_ set the `EnableViewState=true` on the Page and Control? If so, and it still isn't populating on postback, can you provide the control source so we can make sure you saving/loading viewstate in the correct page lifecycle events? – Jon Adams Feb 20 '12 at 19:13
1

Sounds like a job for ControlState:

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

Tim
  • 4,051
  • 10
  • 36
  • 60
  • ControlState can not be turned off. So you want to make sure that all uses of the control will always have need it on. If you don't have full control of the usage of the control, it would be a better idea to save info in the ViewState when possible so that it can be turned off by consumers that don't want to use it. – Jon Adams Feb 20 '12 at 18:42
1

ViewState can be disabled at the application, page, or control level.

To disable at the application level, put the following into your web.config file:

<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:

<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. You can read this excellent reference for more information.

Joe Alfano
  • 10,149
  • 6
  • 29
  • 40
0

Set the ViewStateMode to Disabled on the page, and omit EnableViewState. You can set it to enabled for some controls (default is Inherit).

Edit (sidenote, see comments too):

As we discussed in the comment, text boxes keep their value even though ViewState is disabled. This is true, as they are elements of a HTTP POST request. Labels aren't, for instance, as they render to span tags.

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. If you set ViewStateMode to Disabled, an empty string is written. The Label's state is not kept:

<%@ 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.

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
  • I try your solution but when i see the source code i see view state again.and when my page reload my text boxs have value again. – Mahdi jokar Feb 20 '12 at 18:40
  • You are right. I suppose this is the Control State Tim mentioned. However, I'm not sure to be honest. Anyway, text boxes do keep their text as they are HTML form elements. Their content is POSTed back to the server directly rather than boxed in ViewState. I wrote some sample code, I'll edit my answer. – Matthias Meid Feb 20 '12 at 20:28