How to programmatically determine page and viewstate size ?

  • Thread starter Thread starter NewsAccount
  • Start date Start date
N

NewsAccount

Hi

I'm trying to find a way to programmatically measure the size of the page
sent down to the browser, can't seem to measure the length of what gets
written to the HTML TextWriter in
protected override void Render(HtmlTextWriter writer)
{



Also, I want to find the size of the viewstate when rendering the page. I've
seen a way to get this when posting back, however, I would like to do this
at render time so that I can neatly log this information .

Any ideas ?



Many thanks



Guy
 
You can determine page size by doing this in the Render event (Sorry this
example is in vb.net, but it should be similar in C#)

Dim sw As New System.IO.StringWriter
Dim localWriter As New HtmlTextWriter(sw)
MyBase.Render(localWriter)
Dim output As String = sw.ToString()
Dim size As Long
size = output.Length()
writer.Write(output)


I'm not quite as sure about viewstate, but this page may help you
http://www.aspalliance.com/articleViewer.aspx?aId=135&pId=
 
Thanks very much for the help Michael.

If anyone else is interested here are the C# implementations

To Calculate Viewstate size :
protected override void SavePageStateToPersistenceMedium(object viewState)

{

base.SavePageStateToPersistenceMedium(viewState);

LosFormatter format = new LosFormatter();

StringWriter writer = new StringWriter();

format.Serialize(writer, viewState);

int viewStateLength = writer.ToString().Length;



To Calculate page size :

protected override void Render(HtmlTextWriter writer)

{

StringWriter customWriter = new StringWriter();

HtmlTextWriter localWriter = new HtmlTextWriter(customWriter);

base.Render(localWriter);

int pageLength = customWriter.ToString().Length;

writer.Write(customWriter.ToString());
 
Back
Top