Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Directing the Action with Page Directives


Directing the Action with Page Directives

The internal structure of an ASP.NET page is extremely modular and comprises three distinct sections—page directives, code, and page layout.

The code section includes any source code pertinent to the page, such as event handlers and helper routines. The page layout represents the skeleton of the page and includes server controls, literal text, and HTML tags. What about page directives, then?

In the end, the set of page directives establish the environment in which the page will run. Directives specify how the HTTP runtime should process the page, and determine which assumptions about the page’s behavior are safe to make. Directives also let you import namespaces to simplify coding (the @Import directive), load assemblies not currently in the global assembly cache (the @Assembly directive), and register new controls with custom tag names and namespace prefixes (the @Register directive). The role of a couple more directives—specifically, @Implements and @Reference—is less emphasized in literature; perhaps little used in practice, but still important.

The @Implements directive indicates that the current page implements the specified interface. Implementing an interface in an ASP.NET page is definitely possible and in no way different from implementing the same interface in a class. If you write the page with inline code, you declare any required methods and properties within the <script> section. The syntax of the @Implements directive is as follows:

<%@ Implements interface="InterfaceName" %>

The @Implements directive can appear multiple times in the page if the page has to implement multiple interfaces. Note that if you use the code-behind model, you can't use the directive to implement interfaces. In this case, you simply implement the interface in the code-behind class.

The @Reference directive is used to establish a dynamic link between the current page and the specified page or user control. It enables cross-page communication and lets you create strongly typed instances of user controls. The @Reference directive can appear multiple times in the page and features two mutually exclusive attributes—Page and Control. Both attributes are expected to contain a path to a source file:

<%@ Reference page="source_page" %>
<%@ Reference control="source_user_control" %>

The Page attribute points to an ASP.NET source file, whereas the Control attribute contains the path to a user control. In both cases, the referenced source file will be dynamically compiled into an assembly, thus making the classes defined in the source programmatically available to the referencing page. When the referencing page executes, a referenced page becomes a class that represents the ASPX source file and can be instantiated and programmed at will. Notice that for the directive to work, the referenced page must belong to the same domain as the calling page. Cross-site calls are not allowed, and both the Page and Control attributes expect to receive a relative virtual path.

It is worth noting that, in ASP.NET 2.0, the @Implements directive is used to let a page implement script callbacks, which is the ability to perform lightweight roundtrips to the server to download up-to-date information on the client.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.