Page Templates: Designer Issues
page 1 of 1
Published: 08 Oct 2003
Unedited - Community Contributed
Abstract
I sometimes encounter questions or issues with the Visual Studio .NET Designer when using Page Templates since there are a few things that work differently, and in one case actually results in a loss of intellisense in the Html View. This article will give you solutions, or work-arounds, to all of these issues.
by Paul Wilson
Feedback
Average Rating: 
Views (Total / Last 10 Days): 33105/ 303

Page Templates: Designer Issues

Page Templates: Designer Issues

Paul Wilson
www.WilsonDotNet.com
www.ASPAlliance.com/PaulWilson

Previous Article            Download Demo            Next Article

Overview

I sometimes encounter questions or issues with the Visual Studio .NET Designer when using Page Templates since there are a few things that work differently, and in one case actually results in a loss of intellisense in the Html View. This article will give you solutions, or work-arounds, to all of these issues.

Broken Designer

I sometimes hear that the Designer is completely broken when using Page Templates. This is nearly always caused by one of a few problems that are easily resolved. First, the base Page class must be compiled before viewing a Page in the Designer. Next, the base Page class cannot be marked as abstract (or MustInherit in VB.NET), even though standard OOP would dictate that your base Page class should be abstract. Apparently the Designer actually instantiates the base Page class, probably as part of Reflection, even though it never gives you any visual inheritance as a benefit. Rumors that persist in the various newsgroups that the Designer does not work with Page Templates is simply not the case, and even the minor issues are now solvable.

Missing Meta-Tags

ASPX pages include Meta-tags for GENERATOR, CODE_LANGUAGE, vs_defaultClientScript, and vs_targetSchema by default when the VS.NET wizard creates a new ASPX Page. However, the Meta-tags are usually removed, along other tags, in Page Templates. The GENERATOR and CODE_LANGUAGE Meta-tags are descriptive only, so nothing is lost. The vs_defaultClientScript and vs_targetSchema are used by VS.NET to a degree, but both of these properties have defaults that can be set in the Project Properties. This is accessed by right-clicking the Project in the Solution Explorer window. Go to Designer Defaults in Common Properties and you will see both these properties, as well as the default Page Layout which is removed with Body in Page Templates.
Figure 1: Project Properties

Poor Intellisense

There are also a few issues with intellisense in the Designer with Page Templates, again because the normal Html, Head, Body, and other tags are usually removed. Red squiggles will now be under any root tags on the Page, warning invalid Html, since the Designer is expecting the typical structure of Html, Head, and Body. You can disable Html Validation (Tools-Options:Text Editor-HTML/XML-HTML Specific), but I personally don't find this minor annoyance to be worth turning off a feature. A bigger issue is that the Html intellisense is missing for Web () Controls. However, the Designer still allows you to drag-n-drop these and set their properties. A work-around for Html is to temporarily add the Body tag, then remove it when done.

Major Work-Around

Finally, there is a major work-around for missing Meta-tags and poor intellisense, which will also allow you to view your Page in the Designer if using a StyleSheet. Just add the following override of AddParsedSubObject into your base Page class and then leave the Html, Head, Meta, Link, Body, and other tags in the ASPX page. This code causes the Html parser to simply ignore the first literal block of Html if it begins with the DocType or Html tags, and the last block if it ends with Html. It will also still work if you do remove these blocks so you are safe either way. There is negligible overhead, since it exploits a method already used by the parser, although you may need to modify it for some cases you may use, or if you use VB.NET.
Listing 1: Major Work-Around
protected override void AddParsedSubObject(object obj) {
  bool addControl = true;
  string type = obj.GetType().ToString();
  if (type == "System.Web.UI.LiteralControl"
      || type == "System.Web.UI.ResourceBasedLiteralControl") {
    LiteralControl control = (LiteralControl) obj;
    string html = control.Text.Trim().ToUpper();
    if (firstControl) {
      if (html.StartsWith("<!DOCTYPE") || html.StartsWith("<HTML>")) {
        addControl = false;
      }
    }
    else {
      if (html.EndsWith("</HTML>")) {
        addControl = false;
      }
    }
  }
  if (firstControl) firstControl = false;
  if (addControl) base.AddParsedSubObject(obj);
}

Conclusion

Removing the header and footer tags, as is usually done with Page Templates, may cause some additional minor issues in the Visual Studio .NET Designer. Most of these are easily solvable, and work-arounds exist for the others. Hopefully, VS.NET will provide better support for Page Templates in the future.

Author Bio

Paul Wilson is a software architect in Atlanta, currently with PRG-Schultz. He specializes in Microsoft technologies, including .NET, C#, ASP, SQL, COM+, and VB. His WilsonWebForm Control allows Multiple Forms and Non-PostBack Forms in ASP.NET. He is a Microsoft MVP in ASP.NET and is also recognized as an ASPInsider. He is a moderator on Microsoft's ASP.NET Forums, as well as one of the top posters. He is holds the MCSD, MCAD, MCDBA, and MCSE certifications. Please visit his website, www.WilsonDotNet.com, or email him at Paul@WilsonDotNet.com.


User Comments

Title: Perfect article   
Name: John.net
Date: 2004-08-29 9:12:46 PM
Comment:
This is the perfect article to read if you've surfed all over looking for a better way to use templates than removing html, head, etc tags from your pages. Paul solves the problem. Doesn't this bother anyone except me? Those other solutions are embarrring and I'd never mention them with their implementation shortfalls. If you suggest a cheesey solution like that to me in an interview I'd never hire you!!! This is the implementation that I've been looking for. Thanks Paul :)

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-05-23 2:58:31 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search