In-Depth

Simplify Updates With Templates

ASP.NET developers often use a base class inherited from the root class Page as a template to make their Web sites look consistent. However, the HTML is often embedded in the base class, preventing the template from being previewed or edited. Learn to work around this problem by separating the template and the base class.

ASP.NET developers often use a base class inherited from the root class Page as a template to make their Web sites look consistent. However, the HTML code is often embedded in the base class, preventing you from easily previewing or editing the layout of the template. You can work around these problems by separating the template and the base class.

You store the template that contains HTML and PlaceHolder controls in a user control file (see Listing 1). By default, a user control created by VS.NET doesn't have HTML and BODY tags because it is only part of a page. The good thing is that VS.NET doesn't complain when you add these two tags to the file manually. The advantage of storing the template in a user control file is that you can clearly preview and/or change the layout of the template from VS.NET's Design View (although I personally prefer editing the HTML code directly).

Take a look at the base class code in VB.NET (see Listing 2). In the Page_Init event, you load the template control by using the LoadControl and Add methods. You can reference the PlaceHolder controls in the template by using the FindControl method. The main content is loaded during the Page_Load stage and coded within the Web page class, but other controls (Header, Footer, and so on) are loaded during the Page_PreRender stage.

For any Web page that uses the template, set its class behind to inherit from the base class. The HTML code of the page is wrapped by a Panel control. At the end of the Page_Load, let the parent class (MyBase) load this Panel control. This is a critical step to get rid of a separate user control file (see Listings 3 and 4).

Once the Web page is finished, Web editors can update the content in two ways. For dynamic Web pages, they update content stored in a database by a separate program, and the changes are reflected on the Web page automatically. For manual Web pages, they work on them directly using an HTML editor to change the content as well as the layout.

Don't use subfolders for user control files unless you have a subfolder for the Web pages as well. When these the ASPX files and ASCX files exist in different levels within the Web root directory, the path of the image (or maybe other including) files will be different at design time than run time, making the Web page design and content editing inconvenient. Of course, you can use the full path, such as /folder1/folder2/aimage.jpg, to resolve this issue, but I always use the relative path whenever possible because it's simpler. You can prefix the filename with "uc" to better indicate a file is a user control.

I'm reluctant to set the base page for all the Web pages in the Web.Config file. Most of the Web pages use the same template, but some might not. They either use another template or don't use any template.

About the Author

Yue Xu is a senior Web systems analyst at Reed Business Information in Morris Plains, N.J. Reach Yue at [email protected].

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube