Adding Design-Time Support -Page 10
August 16, 2002
While using the Blog control in a Web Forms page is fairly
simple, it's still not 100 percent intuitive. For example, without
documentation, there's no way for someone using the Blog control to know what
the appropriate values for the Mode property are. Without explicitly telling
developers using the control about the Add mode, it would be difficult for
them to discover and use this mode on their own.
For developers using Visual Studio .NET (or another IDE that
supports IntelliSense), you can solve this problem by adding design-time
support to the control. This is done by using a combination of special
metadata attributes added to the control and custom XSD schemas to support
IntelliSense statement completion for Web Forms pages. IntelliSense support in
code-behind modules is automatic and requires no additional coding.
Part of the challenge of providing design-time support for
custom server controls is that different editors in the Visual Studio IDE
require different techniques to support design-time functionality. Custom
controls automatically support IntelliSense statement completion when working
with code-behind modules in Visual Basic .NET or C#. Figure
6-3 shows this statement completion in action for the Blog control.
Figure 6-3. IntelliSense in code-behind
|
|
Unfortunately, when editing Web Forms pages, automatic support
for statement completion does not extend to the Design or HTML views (nor does
Visual Studio provide built-in support for viewing and editing properties in
the Property browser without additional work in your control). To complicate
things further, one technique is necessary for supporting IntelliSense in the
Property browser and Design view of the Web Forms editor, while another is
necessary for supporting it in the HTML view of the Web Forms editor.
The technique required for supporting property browsing in
Design view uses metadata attributes to inform Visual Studio .NET about how to
handle the properties. Supporting statement completion and property browsing
in HTML view requires creating a custom XSD schema that describes the types in
your control. We'll discuss both techniques in the next sections.
Metadata attributes
Visual Studio .NET provides rich support for designing and
modifying controls visually by using drag-and-drop techniques and tools, such
as the Property browser, and related designers, such as the color picker.
Support for these tools is provided by a series of metadata attributes that
you can add to your control. These attributes tell the Visual Studio IDE
whether to display any properties that your control exposes in the Properties
browser, what type the properties are, and which designer should be used to
set the properties' values.
To support editing of the AddRedirect property in the Property
browser, we would add the following attributes before the Property procedure,
as shown in the following code snippet: [
Browsable(true),
Category("Behavior"),
Description("URL to which the page should redirect after
successful submission of a new Blog entry."),
Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(UITypeEditor))
]
public string AddRedirect
{ // property procedure code }
These attribute declarations allow the property to be displayed
in the Property browser, set the desired category for the property (when
properties are sorted by category), provide a description of the property, and
tell Visual Studio .NET to use the UrlEditor designer to edit the property's
value.
TIP: The attribute syntax shown in this
section is for C#. In C#, attributes take the form:
[AttributeName(AttributeParams)]
In Visual Basic .NET, attributes are declared with the
following syntax:
<AttributeName(AttributeParams)>
Visual Basic .NET requires that the attribute declaration
appear on the same line as the member it's modifying, so it's usually a good
idea to follow the attribute with a VB line continuation character to
improve readability:
<AttributeName(AttributeParams)> _
Public Membername( )
In both C# and VB, you can declare multiple attributes within a single
set of [ ] or <> brackets by separating multiple attributes with
commas.
In addition to setting attributes at the property level, you can
set certain attributes at the class and assembly levels. For example, you can
use the assembly-level attribute TagPrefix to
specify the tag prefix to use for any controls contained in the assembly.
Visual Studio .NET then inserts this tag prefix automatically when you add an
instance of the control to a Web Forms page from the Visual Studio toolbox.
The following code snippet shows the syntax for the TagPrefix attribute. This attribute should be placed
within the class module that defines the control, but outside the class and
namespace declarations. [
assembly: TagPrefix("aspnetian", "aspnetian")
]
namespace aspnetian
{ // control classes, etc. }
To complete the integration of a control in the Visual Studio
.NET environment, add the ToolBoxData attribute
(which tells Visual Studio .NET your preferred tag name for controls inserted
from the toolbox) to the class that implements the control: [
ToolboxData("<{0}:Blog runat=server></{0}:Blog>")
]
public class Blog:Panel, INamingContainer
{ // control implementation }
Once compiled, the control will support automatic insertion of
the @ Register directive, tag prefix, and tag name
for the Blog control. To add the control to the Visual Studio .NET toolbox,
follow these simple steps:
- In Design view, select the Web Forms tab of the Visual
Studio .NET toolbox.
- Right-click anywhere in the tab and select Customize
Toolbox.
- Select the .NET Framework Components tab, and then
click Browse.
- Browse to the location of the compiled control
assembly, select it, and click Open.
- Click OK.
Once the control has been added to the toolbox, you can add it
to a Web Forms page by either double-clicking the control or dragging and
dropping it from the toolbox onto the Web Forms page. In either case, Visual
Studio .NET will automatically insert the correct @
Register directive, including setting the TagPrefix based on the
assembly-level attribute, and will also create a set of tags for the control
with the tag name specified in the ToolBoxData
attribute.
Compositional Controls (Cont.) - Page 9
ASP.NET in a Nutshell
Adding Design-Time Support (Cont.) - Page 11
|