1

I have ViewState disabled on my ASPX page:

<%@ Page Title="Home Page" MasterPageFile="~/Views/Shared/WebForm.master" Language="C#" EnableViewState="False" AutoEventWireup="True" CodeBehind="Dashboard.aspx.cs" Inherits="CableSolve.Web.Dashboard.Dashboard"%>

and I have the following class:

public class DashboardUpdatePanel : UpdatePanel
{
    public DashboardUpdatePanel()
    {
        UpdateMode = UpdatePanelUpdateMode.Conditional;
        CssClass = "maxHeight";
    }

    [Category("Appearance")]
    [Description("The CSS class applied to the UpdatePanel rendering")]
    public string CssClass
    {
        get
        {
            string s = (string)ViewState["CssClass"];
            return s ?? String.Empty;
        }
        set
        {
            ViewState["CssClass"] = value;
        }
    }

    protected override void RenderChildren(HtmlTextWriter writer)
    {
        if (IsInPartialRendering == false)
        {
            string cssClass = CssClass;
            if (cssClass.Length > 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
            }
        }
        base.RenderChildren(writer);
    }
}

I see effects on my page if I remove the assignment to the CssClass property. Yet, the setter is assigning the value to ViewState.

How does this work? Surely if ViewState is disabled I would not see any difference whether I had CssClass assigned to or not.

Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
  • What's the ViewStateMode of the control at runtime? ASP.NET lets you enable or disable it per-control. – millimoose Nov 02 '11 at 19:03
  • 1
    My understanding was that ViewState was hierarchical. That is, if ViewState is disabled at the top-most level then it would be disabled for all children. – Sean Anderson Nov 02 '11 at 19:05
  • That's true; more precisely, the default behaviour for children is to inherit the `ViewStateMode` value. I was asking because my first idea was that something somewhere changes that default, but the answers posted seem more likely, and checking at runtime wouldn't help since it'd just show "Inherit" and you'd have to manually walk up the control tree, so nevermind. – millimoose Nov 02 '11 at 19:19

3 Answers3

3

Disabling ViewState means that the ViewState will not be persisted across postbacks.

Within a postback, ViewState will still work.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Woah. Thank you. Do you have any more reading I can do on this? Also, do you think, knowing this, that I should re-write the above class to use ControlState if I intend on having ViewState disabled? ViewState working within a postback is really subtle. – Sean Anderson Nov 02 '11 at 19:08
2

You are free to use the ViewState bag during the lifecyle of your page even if ViewState is disabled (Disabling ViewState does not render the Page.ViewState property inoperable).

Essentially what you are doing in your property setter is storing the CssClass value in a loosely-typed state bag as opposed to a member variable, with the same effect.

My guess is that you are setting the CSS property in the markup which would mean that the value is getting set each time the page is rendered, so ViewState would not come into play anyway.

<my:control ID="myControl" CssClass="myCssClass" ... />
RoccoC5
  • 4,185
  • 16
  • 20
  • Nope, I removed the mark-up setting when I created my own class for the object -- which is what spurred my surprised when I learned it was setting to ViewState. :) – Sean Anderson Nov 02 '11 at 19:19
0

Controls use ControlState - not ViewState. It's a separate collection although you refer to it by the same name when inside Controls.

Leon
  • 3,311
  • 23
  • 20
  • http://stackoverflow.com/questions/3457136/asp-net-checkbox-does-not-fire-checkedchanged-event-when-unchecking This thread shows using ControlState. It does not reference ViewState. ?? – Sean Anderson Nov 02 '11 at 19:07
  • 1) Disable ViewState on the page. 2) Save something to ViewState inside your control. 3) ViewSource: check what's in the ViewState hidden field on the page. It will not be empty. – Leon Nov 02 '11 at 19:10
  • Sure, but then allow a postback to happen. If ViewState is disabled then the 'something' is lost. But, if it is written to ControlState, it is not lost, I believe. :) – Sean Anderson Nov 02 '11 at 19:13
  • Correct. ViewState hidden field contains a mis-mash of Page + Control information. When you disable it on the page, the controls can still use it and it will work in postbacks. – Leon Nov 02 '11 at 19:14