oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

User Controls and Custom Server Controls, Part 3
Pages: 1, 2, 3, 4, 5

Custom schemas and Visual Studio annotations

As much as the metadata attributes described in the previous section help provide support for the Blog control at design time, they're missing one important piece: IntelliSense support for adding tags and attributes in the HTML view of the Web Forms editor. For developers who are more comfortable working in HTML than in WYSIWYG style, this oversight is significant.

Since the HTML view of the Web Forms editor uses XSD schemas to determine which elements and attributes to make available in a Web Forms page, to correct the oversight, we need to implement an XSD schema that describes the Blog control and the attributes that it supports. Optionally, we can add annotations to the schema that tell Visual Studio .NET about the various elements and how we'd like them to behave.

Example 6-12 contains the portion of the XSD schema specific to the Blog control. The actual schema file (which is available in the sample code for the book) also contains type definitions for the Panel control from which the Blog control is derived, as well as other necessary attribute and type definitions. These definitions were copied from the asp.xsd schema file created for the built-in ASP.NET Server Controls.

WARNING:   You should never modify the asp.xsd schema file directly, but should copy any necessary type or attribute definitions to your custom schema file. While this may seem redundant, if you edit asp.xsd directly and a later installation or service pack for the .NET Framework overwrites this file, your custom schema entries would be lost.

Example 6-12: Blog.xsd

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema targetNamespace="urn:"
   vs:friendlyname="Blog Control Schema"
   vs:requireattributequotes="true" >
         Blog Control schema.
   <xsd:element name="Blog" type="BlogDef" />
   <!-- <aspnetian:Blog> -->
   <xsd:complexType name="BlogDef">
      <!-- <aspnetian:Blog>-specific attributes -->
      <xsd:attribute name="AddRedirect" type="xsd:string"
      <xsd:attribute name="Email" type="xsd:string"/>
      <xsd:attribute name="Mode" type="BlogMode"/>
      <xsd:attribute name="SeparatorColor" type="xsd:string" 
      <!-- <asp:Panel>-specific attributes -->
      <xsd:attribute name="BackImageUrl" type="xsd:anyURI" />
      <xsd:attribute name="HorizontalAlign" type="HorizontalAlign" />
      <xsd:attribute name="Wrap" type="xsd:boolean" />
      <xsd:attribute name="Enabled" type="xsd:boolean" />
      <xsd:attribute name="BorderWidth" type="ui4" />
      <xsd:attribute name="BorderColor" type="xsd:string" 
         vs:builder="color" />
      <xsd:attribute name="BorderStyle" type="BorderStyle" />
      <xsd:attributeGroup ref="WebControlAttributes" />
   <!-- DataTypes -->
   <xsd:simpleType name="BlogMode">
      <xsd:restriction base="xsd:string">
         <xsd:enumeration value="Add" />
         <xsd:enumeration value="Display" />

In Example 6-12, note the targetNamespace and xmlns attributes on the root schema element, which define the XML namespace for the control's schema. The value of the targetNamespace and xmlns attributes will also be used as an attribute in your Web Forms page to "wire up" the schema. The <xsd:element> tag defines the root Blog element. The <xsd:complexType> tag defines the attributes for the Blog element, which includes the web control attributes referenced by the <xsd:attributeGroup> tag. Finally, the <xsd:simpleType> tag defines the enumeration for the BlogMode type used as one of the attributes for the Blog element.

Note that Example 6-12 uses the vs:builder annotation to tell Visual Studio .NET to use the Url builder for the AddRedirect attribute and the Color builder for the SeparatorColor attribute. The vs:builder annotation is one of many annotations available to modify schemas. The most commonly used annotations are listed in Table 6-1.

Table 6-1: Common Visual Studio .NET annotations



Valid values


Used at the root <schema> element to determine whether Visual Studio may insert style attributes for positioning.



Indicates whether leading whitespace may be added to the element during automatic formatting.



Specifies the builder to be used for editing the related property's value.

color, style, or url


Allows a related property to be marked as `deprecated', which prevents it from showing up in the Properties browser and in statement completion.



Used at the element level to indicate that Visual Studio .NET should use single tag syntax for the related tag (no end tag).



Used at the root level to provide a display name for the schema.



Used at the root level and specifies whether Visual Studio .NET will treat the related tags in a case-sensitive manner.



Used at the root level and specifies whether the schema is an HTML document schema.



Used at the attribute level and specifies that the attribute should not appear in statement completion.



Used at the attribute level and specifies that the attribute may not be modified in the Properties window.



Used at the root level and specifies that the attribute values must have quotes.


Once you've built your XSD schema, save it to the same location as the asp.xsd file (which defaults to C:\ProgramFiles\MicrosoftVisualStudio.NET\Common7\Packages\schemas\xml).

Pages: 1, 2, 3, 4, 5

Next Pagearrow