IntentProvide an automatic template framework that ensures that all pages within a site have a consistent appearance. ProblemMany 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. ForcesUse the WebForm Template pattern when:
StructureThe 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.
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. ConsequencesThe WebForm Template has the following benefits and liabilities:
Participants
ImplementationThe 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.
Related Patterns
|