Directives are used to pass optional settings to the ASP.NET pages and compilers. They typically have the following syntax:
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.
Here is a sample Application 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 below.
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:
For example, the following Implements directive allows access to a custom IDataAccess interface contained in a custom ProgrammingASPNET namespace:
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 Page directive has many possible attributes. Some of the more common attributes of the Page directive are listed in Table below.
The following code snippet is a Page directive specifying the language, a class to inherit, and a code-behind source file:
There are two permissible attributes: Page and Control. For either, the allowable value is a relative or fully qualified filename. For example:
If this tutorial doesn't answer your question, and you have a specific question, just ask an expert here. Post your question to get a direct answer.
<%@ 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.
1. Application Directive
The Application directive is used to define application-specific attributes. It is typically the first line in the global.asax file.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 below Attribute | Description |
---|---|
CodeBehind | Used by Visual Studio .NET to identify a code-behind file. |
Inherits | The name of the class to inherit from. |
Description | Text description of the application. This is ignored by the parser and compiler. |
Language | Identifies the language used in any code blocks. Valid values are "C#", "VB", and "JS". As other languages adopt support for the .NET Framework, this list will be expanded. |
2. Assembly Directive
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.
Attribute | Description |
---|---|
Name | The name of the assembly to link to the application or page. Does not include a filename extension. Assemblies usually have a dll extension. |
Src | Path to a source file to dynamically compile and link. |
<%@ Assembly Name="MyAssembly" %>
<%@ Assembly Src="SomeSource.cs" %>
This directive is often used in conjunction with the Import directive, described later in this chapter.3 Control Directive
The Control directive is used only with user controls and is contained in user control files (.ascx). There can only be a single Control directive per .ascx file. Here is an example:<%@ Control Language="VB" EnableViewState="false" %>
The Control directive has many possible attributes. Some of the more common attributes appear in Table below. Attribute | Values | Description |
---|---|---|
AutoEventWireup | true, false | Enables or disables event auto wiring. Default is true. |
ClassName | any valid class name | The class name for the page that will be compiled dynamically. |
Debug | true, false | Enables or disables compiling with debug symbols. Default is false. |
Description | string | Text description of the page, ignored by the parser. |
EnableViewState | true, false | Indicates if view state is maintained across page requests. Default is true. |
Explicit | true, false | If language is VB, tells compiler to use Option Explicit mode. Default is false. |
Inherits | class name | Name of code-behind or other class for the page to inherit. |
Language | VB, C#, JS | Programming language used for in-line code and script blocks. As other languages adopt support for the .NET Framework this list will be expanded. |
Src | filename | Relative or fully qualified filename containing code-behind class. |
Strict | true, false | If language is VB, tells compiler to use Option Strict mode. Default is false. |
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" %>
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:
- System
- System.Collections
- System.Collections.Specialized
- System.Configuration
- System.IO
- System.Text
- System.Text.RegularExpressions
- System.Web
- System.Web.Caching
- System.Web.Security
- System.Web.SessionState
- System.Web.UI
- System.Web.UI.HtmlControls
- System.Web.UI.WebControls
<%@import namespace="System.Drawing" %>
<%@import namespace="ProgrammingASPNET" %>
6 OutputCache Directive
The OutputCache directive controls output caching for a page or user control..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 below.
Attribute | Values | Description |
---|---|---|
AutoEventWireup | true, false | Enables or disables event auto wiring. Default is true. |
Buffer | true, false | Enables or disables HTTP response buffering. Default is true. |
ClassName | Any valid class name | The class name for the page that will be compiled dynamically. |
ClientTarget | Any valid user-agent value or alias | Targets user agent that server controls should render content for. |
CodeBehind | filename | Used by Visual Studio .NET to indicate the name of the code-behind file. |
Debug | true, false | Enables or disables compiling with debug symbols. Default is false. |
Description | string | Text description of the page, ignored by the parser. |
EnableSessionState | true, false, ReadOnly. | Enables, disables, or makes SessionState read-only. Default is true. |
EnableViewState | true, false | Enables or disables maintenance of view state across page requests. Default is true. |
ErrorPage | Targets URL for redirection if an unhandled page exception occurs. | |
Explicit | true, false | If language is VB, tells compiler to use Option Explicit mode. Default is false. |
Inherits | class name | Name of code-behind or other class |
Language | VB, C#, JS | Programming language used for in-line code. |
Src | filename | Relative or fully qualified filename containing code behind class. |
Strict | true, false | If language is VB, tells compiler to use Option Strict mode. Default is false. |
Trace | true, false | Enables or disables tracing. Default is false. |
TraceMode | SortByTime, SortByCategory | Indicates how trace messages are to be displayed. Default is SortByTime. |
Transaction | NotSupported, Supported, Required, RequiresNew | Indicates if transactions supported on this page. Default is NotSupported. |
<%@ Page Language="C#" inherits="CodeBehindDemo" src="codebehind.cs" %>
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.There are two permissible attributes: Page and Control. For either, the allowable value is a relative or fully qualified filename. For example:
<%@ Reference page="AnotherPage.aspx" %>
9 Register Directive
The Register directive is used in custom server controls and user controls to associate aliases with namespaces.If this tutorial doesn't answer your question, and you have a specific question, just ask an expert here. Post your question to get a direct answer.
0 comments:
Post a Comment