I’m working on some mobile forms today and while I’m working on this I’m constantly wondering – what the heck are the ASP.NET Mobile controls rendering? So I created a generic routine that is part of my base page that allows writing the Page output to a file:

 

/// <summary>

/// This method renders page output to a file specified and/or returns the

/// page output as a string.

/// </summary>

/// <param name="writer"></param>

/// <param name="OutputFile"></param>

/// <returns></returns>

protected string SavePageOutput(HtmlTextWriter writer, string OutputFile)

{

    // *** Write the HTML into this string builder

    StringBuilder sb = new StringBuilder();

    StringWriter sw = new StringWriter(sb);

 

    MobileTextWriter hWriter = (MobileTextWriter) this.Adapter.CreateTextWriter(sw);

    base.Render(hWriter);

 

    // *** store to a string

    string PageResult = sb.ToString();

 

    // *** Write it back to the server

    writer.Write(PageResult);

 

    if (OutputFile != null && OutputFile != "")

    {

        StreamWriter ssw = new StreamWriter(OutputFile);

        ssw.Write(PageResult);

        ssw.Close();

    }

   

    return PageResult;

}

 

To write the output of a page to disk all I have to then do is something like this:

 

protected override void Render(HtmlTextWriter writer)

{

#if (SavePageOutput)

    this.SavePageOutput(writer, Request.PhysicalApplicationPath +

                        "admin\\mobile\\ShowInvoices_Output.htm");

#else

    base.Render(write)

#endif

}

 

Of course you could just as easily write the output somewhere else – like a database… Super useful for all sorts of occasions.

 

I guess I could work this directly into my page framework by having an optional capture file property on the Page class. But let's not get too fancy <g>...