|
Let Users Save From ASP.NET
Export the content of your ASP.NET pages to standard Office document types to avoid having to create separate reports.
by Brian Eshelman
December 6, 2004
Technology Toolbox: C#, ASP.NET
Your users will want to save data from your pages at some point in your Web site's lifecycle. Typical Web pages neither save well nor print well, so whole product lines have evolved to generate reports to get data out of a system.
These products do their jobs well, but they require a completely separate development effort. Fortunately, you can simply export your existing pages rather than create a separate system. Simply plug into the ASP.NET framework to intercept the HTML you generate, then reuse the work others have done already to convert HTML to the document formats your users are accustomed to using, such as Microsoft Word, Microsoft Excel, or even Adobe's PDF format. Implementing your site in this manner can help you eliminate the need for a separate reporting system within your site.
You need to put only a few elements in place to start exporting your existing ASP.NET code. First, you must give the user the option to export the desired page to the approved document formats. Second, you must intercept the HTML your page generates and convert it to the appropriate document format. Finally, you need to send the converted document rather than the original HTML to the user.
You spread the code to do this across four elements: the Web page, the export provider manager, the export provider, and the list of exports defined for the page. The Web page registers the list of exports with the manager. The manager generates the options and intercepts the HTML, but the Web page must pass control to the manager for this to occur. The manager also instantiates the appropriate export provider, and that provider streams the converted document to the client (see Figure 1).
Note that you can plug the export functionality into the architecture at any point where you can intercept the HTML and alter the stream back to the client. I chose the templated Web page approach to keep interaction with the page as simple as possible.
Your Web site should utilize a common base class. In addition to providing the coordination with the export provider manager, this base page can provide a consistent look and feel through any number of template implementations. The approach described here uses a derivative of the WebForm Template pattern documented in Christian Thilmany's .NET Patterns: Architecture, Design, and Process to implement a common base page for the site (see Additional Resources).
Override the Render Method
The general idea of the WebForm Template pattern is to use the base class to derive from the standard System.Web.UI.Page and then override the Render method. The override provides a standard header and footer for each page, including logo, menus, or copyrights. TemplateTop and TemplateBottom methods exist in this base class to generate and return HTML strings of the opening and closing of the HTML document, respectively. The Render method's override requires only a couple lines of code:
protected override void
Render(HtmlTextWriter writer)
{
writer.Write(TemplateTop());
base.Render(writer);
writer.Write(TemplateBottom());
}
When developing new pages, you remove the standard opening and closing HTML tags generated when you add a new Web form to your project. Also, you change the base class of the page to the templated base page and need to worry only about the content you want displayed on the page.
Back to top
|