mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 906, comments - 722, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

ASP.NET 4.0: ViewStateMode…giving you more control over ViewState!

ViewState is a pain…there, I’ve said it! What causes the most pain with ViewState is the fact that people misuse it a lot, leaving it enabled for every control in  a page when in fact you only really need it for that one control which really needs to persist it’s contents to the next page. Dave Reed a developer on the ASP.NET team has written a truly epic post on ‘Truly Understanding ViewState’, and before you read what comes next, you really should head off and read that first…

Back?

Ok, well now you understand that you should only use ViewState for those controls that actually need it. But there’s a problem…what you’d really like to do is turn off ViewState for an entire page then just turn it on again for the controls that need it, sorry but I have some bad news, you can’t do this in any version of ASP.NET up to and including ASP.NET 3.5 SP1. I also have some good news…you will be able to do this when ASP.NET 4.0 comes out! The magic little property on all ASP.NET Controls which lets you do this  is called ViewStateMode. (Incidentally, this was Dave’s idea and the samples below are a shameless rip-off from him :-))

Put very simply, ViewStateMode has 3 possible values, Enabled, Disabled and Inherit. Which are pretty obvious in their function…Enabled turns saving ViewState on for that control (or any child controls which are set as ‘Inherit’ or have nothing set), Disabled turns it off again, and Inherit says ‘use whatever my parent is set to’. That’s really all there is to it!

Here’s a very simple sample of how this actually works:   

   <form id="form1" runat="server">   

<script runat=”server”>

    protected override void OnLoad(EventArgs e) {

        if (!IsPostBack) {

            label1.Text = label2.Text = "[DynamicValue]";

        }

        base.OnLoad(e);

    }

</script>

<asp:PlaceHolder ID="PlaceHolder1" runat="server" ViewStateMode="Disabled">

    Disabled: <asp:Label ID="label1" runat="server" Text="[DeclaredValue]" /><br />

    <asp:PlaceHolder ID="PlaceHolder2" runat="server" ViewStateMode="Enabled">

        Enabled: <asp:Label ID="label2" runat="server" Text="[DeclaredValue]" />

    </asp:PlaceHolder>

</asp:PlaceHolder>

<hr />

<asp:button ID="Button1" runat="server" Text="Postback" />

As you can see in this sample we’re Disabling ViewState for the PlaceHolder1 control, the child label1 Inherits this property (as Inherit is default) and therefore saves no ViewState…In PlaceHolder2 we set ViewStateMode to ‘Enabled’, which leads to label2  inheriting this property and therefore saves ViewState…On the first page load we set both labels to the output the text [DynamicValue]…

The effect of all this is that, when the Page first loads you see the labels rendered as follows:

Disabled: [DynamicValue]

Enabled: [DynamicValue]

But, do a PostBack and you see:

Disabled: [DeclaredValue]

Enabled: [DynamicValue]

As you’d now expect, the first label hasn’t preserved the value we set into ViewState…the second label has!

What’s really cool is the fact that you can also do this:

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

Remember, in WebForms Page is just another control…it acts as the parent control for every other control in the page. This means that no control will save ViewState unless you set ViewStateMode to Enabled for that control or a control further up it’s control hierarchy (so, a great use would be for ContentPlaceHolders in MasterPages…).

Told you it was simple!

Print | posted on Wednesday, January 28, 2009 7:55 AM | Filed Under [ ASP.NET 4.0 ]

Feedback

# re: ASP.NET 4.0: ViewStateMode…giving you more control over ViewState!

I think I like this but it definitely overlaps the functionality of the EnableViewState property. The only difference I see is that you can override the parent's property value.

Paul
2/10/2009 11:03 PM | Paul Litwin

# re: ASP.NET 4.0: ViewStateMode…giving you more control over ViewState!

very nice and useful post
5/18/2009 6:06 AM | Air Purifiers

# re: ASP.NET 4.0: ViewStateMode…giving you more control over ViewState!

What a great tips that you share with us. I hope I can learn more from your blog.
6/9/2009 9:58 AM | Air Purifier
Gravatar

# re: ASP.NET 4.0: ViewStateMode…giving you more control over ViewState!

Nice article.The code works fine and is a good starting point for me making a custom module.Thanks for the module..<a href= http://www.topinternetkeno.com">Internet keno games
6/24/2009 2:19 AM | Internet keno games
Gravatar

# re: ASP.NET 4.0: ViewStateMode…giving you more control over ViewState!

I just want you to know that you really made a nice review about BlogEngine.Net! Your post really contains an informational message that gave me a lot of knowledge about BlogEngine.
6/24/2009 3:59 AM | http://www.freetexholdem.com
Comments have been closed on this topic.

Powered by: