Mahdi jokar
Mahdi jokar

Reputation: 1267

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?

I have tried:

But how do I enable some controls to still allow viewstate?

I am using .NET 4.

Upvotes: 4

Views: 16121

Answers (5)

Mahdi jokar
Mahdi jokar

Reputation: 1267

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>

Upvotes: 7

Matthias Meid
Matthias Meid

Reputation: 12513

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.

Upvotes: 0

Jon Adams
Jon Adams

Reputation: 25137

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.

Upvotes: 4

Joe Alfano
Joe Alfano

Reputation: 10289

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.

Upvotes: 1

Tim
Tim

Reputation: 4101

Sounds like a job for ControlState:

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

Upvotes: 1

Related Questions