views:

34

answers:

3

What ASP.NET page lifecycle event can I write code in to determine the size of the viewstate that being sent out? Also, is it possible to determine the size without parsing through the rendered HTML (like a property on the page object) or is parsing the only way?

What I'd like to do is log the sizes, specifically if they cross a certain threshold.

A: 

You could use SaveStateCompleted, which occurs right after all the state has been saved to the viewstate. To know the size of the viewstate, just make a character count after calling ToString on the viewstate.

ViewState.ToString.Count() 
Josias
@josias, stepping through it in reflector, that gets called before the page gets rendered, no? And since it hasn't been written yet, you can't know for sure what the size will be, or am I missing something? back to the reflector!
drachenstern
+2  A: 

You can go on the function that is going to writing the viewstate, the SavePageStateToPersistenceMedium. This is the function that also used to compress viewstate...

For example...

public abstract class BasePage : System.Web.UI.Page
{
    private ObjectStateFormatter _formatter = new ObjectStateFormatter();

    protected override void SavePageStateToPersistenceMedium(object viewState)
    {
        MemoryStream ms = new MemoryStream();    
        _formatter.Serialize(ms, viewState);    
        byte[] viewStateArray = ms.ToArray();

        ....

    }
}

Some reference.
http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx
http://forums.asp.net/p/1139883/3836512.aspx
http://www.dotnetcurry.com/ShowArticle.aspx?ID=67&AspxAutoDetectCookieSupport=1

Aristos
Are you suggesting he override this method in particular? That would work. But then he has to inherit that in every page, right? How would he do it without reworking every page? ~ Granted, adding something to every page as an OnSaveCompleted (or whatever) event is going to require re-working every page too, so ... /facepalm
drachenstern
@drachenstern You create the BasePage, and then on the codebehind you declare as parent not the System.Web.UI.Page, but the BasePage. Yes you need to change that, on all pages (not that bad)... I think that there is also one automatic way but at this moment I am not 100% sure to answer if can fit...
Aristos
@Aristos ~ Yeah, we're thinking the same thing (I do the same with my MasterPages ... I have some code that's shared amongst them all)
drachenstern
A: 

I think, after looking at the Reflector'd mechanism for Page and how it handles viewstate, that you're going to have to go to an HttpModule to get what you're after, if you want the actual viewstate size on the page.

I say this because you're going to have to get the literal string from the page after it's been rendered, which doesn't happen till after all the user-definable events have triggered. See reflector output below (partial):

            this.PerformPreRenderComplete();
            if (context.TraceIsEnabled)
            {
                this.Trace.Write("aspx.page", "End PreRenderComplete");
            }
            if (context.TraceIsEnabled)
            {
                this.BuildPageProfileTree(this.EnableViewState);
                this.Trace.Write("aspx.page", "Begin SaveState");
            }
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER, this._context.WorkerRequest);
            }
            this.SaveAllState();
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE, this._context.WorkerRequest);
            }
            if (context.TraceIsEnabled)
            {
                this.Trace.Write("aspx.page", "End SaveState");
                this.Trace.Write("aspx.page", "Begin SaveStateComplete");
            }
            this.OnSaveStateComplete(EventArgs.Empty);
            if (context.TraceIsEnabled)
            {
                this.Trace.Write("aspx.page", "End SaveStateComplete");
                this.Trace.Write("aspx.page", "Begin Render");
            }
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER, this._context.WorkerRequest);
            }
            if (str != null)
            {
                this.ExportWebPart(str);
            }
            else
            {
                this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output));
            }
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE, this._context.WorkerRequest);
            }
            if (context.TraceIsEnabled)
            {
                this.Trace.Write("aspx.page", "End Render");
            }
            this.CheckRemainingAsyncTasks(false);

Otherwise, you can grab the viewstatebag and iterate over it's contents. That works well too, depending on how much detail you want to go into.

drachenstern