Webform Template


Intent

Provide an automatic template framework that ensures that all pages within a site have a consistent appearance.

Problem

Many approaches exist to keep a consistent look and feel between all the pages in a site. Unfortunately, most approaches tend to require a significant level of coordination or setup to implement, either initially or on each page.

This pattern is intended to simplify creating the consistent interface, as well as integrated standard error handling.

Forces

Use the WebForm Template pattern when:

  • You have a Web site for which you want to keep a consistent look across all the pages

Structure

The WebForm Template pattern (Figure 3.10) places most of the implementation in a base class for all Web forms in your site. This base class overrides the System.Web.UI.Control's Render method to take control of how the page is sent to the browser. Through this approach, we are able to add a consistent template to all pages.

Figure 3.10. Rendering the WebForm Template ”simple version.

graphics/03fig10.gif

In addition, standard error handling is provided through a SetError method that will allow all page-handling exceptions to be handled in the same way. In our sample, we set our PageMode to error when an exception is handled to indicate that a standard error message should be included in the HTML output.

Consequences

The WebForm Template has the following benefits and liabilities:

  1. All standard look and feel can be automatically included by a base class . The developer needs only to "clean" the default implementation provided by Visual Studio and change the base class of the code behind the class.

  2. Any additional variants can be easily added . Variations from the standard look and feel can be easily implemented by adding properties for the derived classes to set. For example, a printable page mode could be added that replaces the standard templates with the absolute minimum necessary to provide a "better" page for printing.

  3. Error handling can be easily standardized . In our case, we use the Error Cross-Reference Generator to allow the user to help the support personnel in diagnosing the error.

  4. Visual Studio does not like you to remove the main HTML tags . This is more of an annoyance than anything else, but because this pattern requires you to remove the HTML, HEAD, and BODY tags, an HTML error will always exist in the task list, at a minimum. At worst, I have experienced a couple of instances where this caused the design environment not to work as expected.

Participants

  • WebForm Template ” By serving as a replacement base class for all Web forms, this class enforces the consistent look and feel across the site.

  • Derived Classes ” Each Web form in our site derives from the WebForm Template base class.

  • All HTML other than the content specific to the page is removed.

  • Error Cross-Reference Generator ” Provides the incident ID to be displayed to the user to help any issues be diagnosed and addressed.

Implementation

The TemplateBase class takes advantage of the fact that the output for all WebForms ultimately goes through the virtual Control.Render method. Because of this, we can override the default implementation to surround the page with a consistent page frame. In its simplest form, the code writes the top portion of the template, renders the page, and writes out the bottom portion of the template.

Listing 3.12 Rendering the WebForm Template ”simple version.
 writer.Write(TemplateTop()); base.Render(writer); writer.Write(TemplateBottom()); 

In our case, we add the ability to send a standard error message to the page. This can be initiated by either the derived class or an exception thrown during rendering of the page. The derived class might set the PageMode to err by calling SetError at some point in the Page Load or Web form event processing. The rendering of the page is included in a separate try/catch block to ensure that any exceptions that might occur while rendering the page are caught, and the standard error message can be included.

Listing 3.13 Rendering the WebForm Template ”more complete.
 writer.Write(TemplateTop());    try    {       if (PageMode == Mode.Normal)          base.Render(writer);    }    catch(Exception ex)    {       SetError("Error rendering page (AAB)", ex);    }    if (PageMode == Mode.Error)       writer.Write(ShowErrorSection()); writer.Write(TemplateBottom()); 

The initial setup of the pattern requires the initial template to be designed and "coded" into the template top and template bottom functions. Both functions make use of the .NET StringBuilder class to build a stream of HTML. Any variations or options of the standard template are simply included in the appropriate function. The sample implementation shows how the Page Title is included, both in the HTML head and the header of the page.

Each new Web form to be developed is added to the project in the standard way. Once added, all code other than the first line that includes the <% Page %> tag is removed (Figure 3.11). The base class of the code behind the class is then changed from System.Web.UI.Page to TemplateBase. The content of the page is then designed as any other page in the Visual Studio designer.

Figure 3.11. WebForm Template's default sample page.

graphics/03fig11.jpg

Related Patterns

  • Error Cross-Reference Generator (Eshelman)



.NET Patterns. Architecture, Design, and Process
.NET Patterns: Architecture, Design, and Process
ISBN: 0321130022
EAN: 2147483647
Year: 2003
Pages: 70

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net