Thursday, August 10, 2006

ASP.NET Display ViewState-Size

The ViewState in ASP.NET is saved in a hidden formfield by default. His size can be dangerous high, even though ASP.NET 2.0 has cut the ViewState in half because of better (thanks to the new formatter class, ObjectStateFormatter). If ViewState Size is to high and you get problems passing the firewall, take a look at this.

Here a solution to display the current size of the ViewState inside the browsers statusbar:


protected override void OnPreRenderComplete(EventArgs e)

{

    if (DebugFlag)

    {

        this.ClientScript.RegisterClientScriptBlock(

            this.GetType(),

            "DisplayViewState",

            "<script>window.status = 'ViewState: ' + document.forms[0]['__VIEWSTATE'].value.length + ' bytes';</script>");

    }

}



Put this code in your CodeBehind or in your Web-Base-Page (a page derived from System.Web.UI.Page).

If you are not happy with your ViewState Size, take a look at ViewState Numbers and Solutions to reduce ViewState Size as documented here. Keeping the ViewState on the Server (e.g. via SessionState) instead of the client is one way.

No comments: