<%@ directive attribute=value [attribute=value] %>
There are many valid types of directives, which will be described in detail in the following sections. Each directive can have one or more attribute/value pairs, unless otherwise noted. Attribute/value pairs are separated by a space character. Be careful not to have any space characters surrounding the equal sign (=) between the attribute and its value.
Directives are typically located at the top of the appropriate file, although that is not a strict requirement. For example, Application directives are at the top of the global.asax file, and Page directives are at the top of the .aspx files.
6.4.1 Application Directive
The Application directive is used to define application-specific attributes. It is typically the first line in the global.asax file, which is described fully in Chapter 20.
Here is a sample Application directive:
<%@ Application Language="C#" Codebehind="Global.asax.cs"\ Inherits="WebApplication1.Global" %>
There are four possible attributes for use in the Application directive, which are outlined in Table 6-5.
6.4.2 Assembly Directive
The Assembly directive is contained in either the global.asax file, for application-wide linking, or in a page (.aspx) or user control (.ascx) file, for linking to a specific page or user control. There can be multiple Assembly directives in any file. Each Assembly directive can have multiple attribute/value pairs.
Assemblies located in the \bin subdirectory under the application's virtual root are automatically linked to the application and do not need to be included in an Assembly directive. There are two permissible attributes, listed in Table 6-6.
For example, the following Assembly directives link to the assembly or assemblies contained in the MyAssembly.dll file, and compile and link to a C# source code file named SomeSource.cs:
<%@ Assembly Name="MyAssembly" %> <%@ Assembly Src="SomeSource.cs" %>
This directive is often used in conjunction with the Import directive, described later in this chapter.
6.4.3 Control Directive
<%@ Control Language="VB" EnableViewState="false" %>
The Control directive has many possible attributes. Some of the more common attributes appear in Table 6-7.
6.4.4 Implements Directive
The Implements directive is used in page (.aspx) and user control (.ascx) files or associated code-behind files. It specifies a COM+ interface that the current page implements. This allows a page or user control to declare the interface's events, methods, and properties.
For example, the following Implements directive allows access to a custom IDataAccess interface contained in a custom ProgrammingASPNET namespace:
<%@ Implements Interface="ProgrammingASPNET.IDataAccess" %>
6.4.5 Import Directive
The Import directive imports a namespace into a page, user control, or application, making all the classes and namespaces of the imported namespace available. It is analogous to the using statement in C# and the Imports statement in VB.NET. Imported namespaces can either be part of the .NET Framework Class Library or custom.
If the Import directive is contained in global.asax , then it applies to the entire application. If it is in a page (.aspx) or user control (.ascx) file, then it only applies to that page or user control.
Each Import directive can have only a single namespace attribute. If you need to import multiple namespaces, use multiple Import directives.
The following namespaces are automatically imported into all pages and user controls and do not need to be included in Import directives:
The following two lines import the System.Drawing namespace from the .NET Base Class Library and a custom namespace:
<%@import namespace="System.Drawing" %> <%@import namespace="ProgrammingASPNET" %>
6.4.6 OutputCache Directive
The OutputCache directive controls output caching for a page or user control. Chapter 18 discusses caching and the use of the OutputCache directive.
6.4.7 Page Directive
The Page directive is used to define attributes for the page parser and compiler specific to the page (.aspx) file. There can be no more than one Page directive for each page file. Each Page directive can have multiple attributes.
The Page directive has many possible attributes. Some of the more common attributes of the Page directive are listed in Table 6-8.
The following code snippet is a Page directive specifying the language, a class to inherit, and a code-behind source file:
<%@ Page Language="C#" inherits="CodeBehindDemo" src="codebehind.cs" %>
6.4.8 Reference Directive
The Reference directive can be included in a page file (.aspx). It indicates that another page or user control should be compiled and linked to the current page, giving you access to the controls on the linked page or user control as part of the ControlCollection object.
<%@ Reference page="AnotherPage.aspx" %>
6.4.9 Register Directive
The Register directive is used in custom server controls and user controls to associate aliases with namespaces. Chapter 14 discusses custom server controls and user controls.