Jan 10 2007

No viewstate at all

Posted by admin under ASP.NET 2.0

For the "experimental" site http://www.findfreefonts.net I am developing I highly depend on javascript and server "state" is therefore never an issue - state is either implicitly defined  in the url.

So I didn't need any viewstate at all. You know the

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwU.....LONGLONGLONG...">

stuff inserted into your resulting html page.

I had it on my todo list but never gave it any attentio. Simple thing right - just



<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" EnableViewState="false" %>


right?

Well it does work, kind of... You will now end up with a much shorter viewstate (and the controls are not wasting time serializing it's state  ), but it's still there.

Turns out that ASP.NET always add some sort of value to the viewstate - and the only way of getting rid of that is to override some functions:



    protected override void SavePageStateToPersistenceMedium(
    object viewState)
    {


    }

    protected override object LoadPageStateFromPersistenceMedium()
    {

        return null;

    }

Please note that you need to do it in the pages - not the master page!

These functions are more typically used to alter the way (or medium) viewstate is save to/from - but by just doing nothing at all we now end up with an empty viewstate.

The inoput control is still there - but for me it was good enough. However if you want to get rid of it totally maybe this article might help you out - it is a technique I use to modify the HTML after it has been rendrered but before it's sent to the client browser.