New Document
Directives in ASP.net

ASP.Net directives are instructions to specify optional settings, such as registering a custom control and page language. These settings describe how the web forms (.aspx) or user controls (.ascx) pages are processed by the .Net framework.


Directives specify settings that are used by the page and user-control compilers when the compilers process ASP.NET Web Forms pages (.aspx files) and user control (.ascx) files.

ASP.NET treats any directive block (<%@ %>) that does not contain an explicit directive name as an @ Page directive (for a page) or as an @ Control directive (for a user control).

For syntax information and descriptions of the attributes that are available for each directive, use the links that are listed in the following table.

The syntax for declaring a directive is:

<%@  directive_name 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.

In this section, we will just introduce the ASP.Net directives and we will use most of these directives throughout the tutorials.

The Application Directive

The Application directive defines application-specific attributes. It is provided at the top of the global.aspx file.

The basic syntax for a sample Application directive is:

<%@ Application Language="C#" %>

The attributes of the Application directive are:

AttributesDescription
Inherits the name of the class from which to inherit
Description text description of the application. Parsers and compilers ignore this
Language language used in code blocks
The Assembly Directive

The Assembly directive links an assembly to the page or the application at parse time. This could appear either in the global.asax file for application-wide linking or in the page file or a user control file for linking to a page or user control.

The basic syntax for a sample Assembly directive is:

<%@ Assembly Name ="myassembly" %>

The Assemblydirective links an assembly to the application or page at parse-time. It is analogous to the /reference: command-line switch used by the C# and VB.NET command-line compilers.

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 below.


The attributes of the Assembly directive are:

AttributesDescription
Name the name of the assembly to be linked
Src the path to the source file to be linked and compiled dynamically
The Control Directive

The Control directive is used with the user controls and appears in the user control (.ascx) files.

The basic syntax for a sample Control directive is:

<%@ Control Language="C#"  EnableViewState="false" %>

The attributes of the Control directive are:

AttributesDescription
AutoEventWireupthe Boolean value that enables or disables automatic association of events to handlers
ClassNamefile name for the control
Debugthe Boolean value that enables or disables compiling with debug symbols
Descriptiontext description of the control page, ignored by compiler
EnableViewStatethe Boolean value that indicates whether view state is maintained across page requests
Explicitfor VB language, tells the compiler to use Option Explicit mode
Inheritsthe class from which the control page inherits
Languagelanguage for code and script
Srcthe filename for the code-behind class
Strictfor VB language, tells the compiler to use the Option Strict mode
The Implements Directive

The Implement directive indicates that the web page, master page or user control page must implement the specified .Net framework interface.

The basic syntax for an Implements directive is:

<%@ Implements  Interface="interface_name" %>
The Import Directive

The Import directive imports a namespace into a web page, user control pate of application. If the Import directive is specified in the global.asax, then it will apply to the entire application. If it is in a page of user control page, then it would apply to that page or control.

The basic syntax for an Import directive is:

<%@ namespace="System.Drawing" %>
The Master Directive

The Master directive specifies a page file as being the mater page.

The basic syntax for a sample MasterPage directive is:

<%@ MasterPage Language="C#"  AutoEventWireup="true" 
    CodeFile="SiteMater.master.cs" Inherits="SiteMaster"  %>
The MasterType Directive

The MasterType directive assigns a class name to the Master property of a page, to make it strongly typed.

The basic syntax for a MasterType directive is:

<%@ MasterType attribute="value"[attribute="value" ...]  %>
The OutputCache Directive

The OutputCache directive controls the output caching policies of a web page or a user control. We will discuss this directive in details, in data caching.

The basic syntax for a OutputCache directive is:

<%@ OutputCache Duration="15" VaryByParam="None"  %>
The Page Directive

The Page directive defines the attributes specific to the page file for the page parser and the compiler.

The basic syntax for a Page directive is:

<%@ Page Language="C#"  AutoEventWireup="true" 
    CodeFile="Default.aspx.cs" Inherits="_Default"  Trace="true" %>

The attributes of the Page directive are:

AttributesDescription
AutoEventWireupthe Boolean value that enables or disables Page events that are being automatically bound to methods; for example, Page_Load
Bufferthe Boolean value that enables or disables HTTP response buffering
ClassNameclass name for the page
ClientTargetthe browser for which server controls should render content
CodeFilename of the code behind file
Debugthe Boolean value that enables or disables compilation with debug symbols
Descriptiontext description of the page, ignored by the parser
EnableSessionStateenables, disables or makes session state read-only
EnableViewStatethe Boolean value that enables or disables view state across page requests
ErrorPageURL for redirection if an unhandled page exception occurs
Inheritsthe name of the code behind or other class
Languageprogramming language for code
Srcfile name of the code behind class
Traceenables or disables tracing
TraceModeindicates how trace messages are to be displayed - sorted by time or category
Transactionindicates if transactions are supported
ValidateRequest the Boolean value that indicates whether all input data is validated against a hardcoded list of values
The PreviousPageType Directive

The PreviousPageType directive assigns a class to a page, so that the page is strongly typed.

The basic syntax for a sample PreviousPagetype directive is:

<%@ PreviousPageType attribute="value"[attribute="value" ...]   %>
The Reference Directive

The Reference directive indicates that another page or user control should be compiled and linked to the current page.

The basic syntax for a sample Reference directive is:

<%@ Reference Page ="somepage.aspx" %>
The Register Directive

The Register derivative is used for registering the custom server controls and user controls.

The basic syntax for a sample Register directive is:

<%@ Register Src="~/footer.ascx" TagName="footer" 
    TagPrefix="Tfooter" %>

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.


<%@ directive attribute=value [attribute=value] %>


Previous                                                                                                                                                       Next

Back to Top