By: Dan Hurwitz and Jesse Liberty in Asp.net Tutorials on 2009年02月27日 [フレーム]
Directives are used to pass optional settings to the ASP.NET pages and compilers. They typically have the following syntax:
<%@ 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.
The Applicationdirective is used to define application-specific attributes. It is typically the first line in the global.asaxfile.
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. |
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.asaxfile, 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. |
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.
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. |
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" %>
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" %>
The OutputCache directive controls output caching for a page or user control..
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. |
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" %>
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" %>
The Register directive is used in custom server controls and user controls to associate aliases with namespaces.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Asp.net )
AmbiguousMatchException: The request matched multiple endpoints.
Microsoft.Identity vs Microsoft.IdentityModel.Clients.ActiveDirectory
Getting values from appsettings.json ASP.NET
Pagination in ASP.net core application
Things to note when changing a function to async in your controller
Passing a model globally to all Views in your Asp.net webapp
HttpError is not found in Asp,net core project
Latest Articles (in Asp.net)
Things to note when changing a function to async in your controller
AmbiguousMatchException: The request matched multiple endpoints.
Call an Action in a controller when user clicks a button in View
Button that is only clickable when the checkbox is checked
Pass the same model to multiple views within the same controller
Passing a model globally to all Views in your Asp.net webapp
Pagination in ASP.net core application
Microsoft.Identity vs Microsoft.IdentityModel.Clients.ActiveDirectory
Things to note when changing a function to async in your controller
AmbiguousMatchException: The request matched multiple endpoints.
Call an Action in a controller when user clicks a button in View
Button that is only clickable when the checkbox is checked
Pass the same model to multiple views within the same controller
Passing a model globally to all Views in your Asp.net webapp
Pagination in ASP.net core application
Microsoft.Identity vs Microsoft.IdentityModel.Clients.ActiveDirectory
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate