NET指令是指定可選設置(如註冊自定義控制項和頁面語言)的指令。這些設置描述.Net框架如何處理web窗體(.aspx)或用戶控制項(.ascx)頁。
聲明指令的語法爲:
<%@ directive_name attribute=value [attribute=value] %>
在本節中,我們將只介紹ASP.NET指令,並在整個教程中使用這些指令中的大部分。
The Application Directive
Application指令定義特定於應用程式的屬性。它位於global.aspx文件的頂部。
應用程式指令的基本語法是:
<%@ Application Language="C#" %>
應用程式指令的屬性是:
Attributes | Description |
---|---|
Inherits | The name of the class from which to inherit. |
Description | The text description of the application. Parsers and compilers ignore this. |
Language | The language used in code blocks. |
The Assembly Directive
Assembly指令在分析時將程序集連結到頁面或應用程式。這可以出現在global.asax文件中用於應用程式範圍的連結,也可以出現在頁面文件中用於連結到頁面或用戶控制項的用戶控制項文件中。
彙編指令的基本語法是:
<%@ Assembly Name ="myassembly" %>
Assembly指令的屬性是:
Attributes | Description |
---|---|
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
control指令與用戶控制項一起使用,並顯示在用戶控制項(.ascx)文件中。
控制指令的基本語法是:
<%@ Control Language="C#" EnableViewState="false" %>
控制指令的屬性是:
Attributes | Description |
---|---|
AutoEventWireup | The Boolean value that enables or disables automatic association of events to handlers. |
ClassName | The file name for the control. |
Debug | The Boolean value that enables or disables compiling with debug symbols. |
Description | The text description of the control page, ignored by compiler. |
EnableViewState | The Boolean value that indicates whether view state is maintained across page requests. |
Explicit | For VB language, tells the compiler to use option explicit mode. |
Inherits | The class from which the control page inherits. |
Language | The language for code and script. |
Src | The filename for the code-behind class. |
Strict | For VB language, tells the compiler to use the option strict mode. |
The Implements Directive
Implement指令指示網頁、母版頁或用戶控制項頁必須實現指定的.Net framework接口。
implements指令的基本語法是:
<%@ Implements Interface="interface_name" %>
The Import Directive
Import指令將命名空間導入到應用程式的web頁、用戶控制頁中。如果在global.asax文件中指定了Import指令,則它將應用於整個應用程式。如果它位於用戶控制項頁的某個頁中,則應用於該頁或控制項。
import指令的基本語法是:
<%@ namespace="System.Drawing" %>
The Master Directive
Master指令指定頁文件作爲主頁。
母版頁指令示例的基本語法爲:
<%@ MasterPage Language="C#" AutoEventWireup="true" CodeFile="SiteMater.master.cs" Inherits="SiteMaster" %>
The MasterType Directive
MasterType指令爲頁的Master屬性指定類名,使其成爲強類型。
MasterType指令的基本語法是:
<%@ MasterType attribute="value"[attribute="value" ...] %>
The OutputCache Directive
OutputCache指令控制網頁或用戶控制項的輸出緩存策略。
OutputCache指令的基本語法是:
<%@ OutputCache Duration="15" VaryByParam="None" %>
The Page Directive
Page指令爲頁面解析器和編譯器定義特定於頁面文件的屬性。
Page指令的基本語法是:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Trace="true" %>
Page指令的屬性是:
Attributes | Description |
---|---|
AutoEventWireup | The Boolean value that enables or disables page events that are being automatically bound to methods; for example, Page_Load. |
Buffer | The Boolean value that enables or disables HTTP response buffering. |
ClassName | The class name for the page. |
ClientTarget | The browser for which the server controls should render content. |
CodeFile | The name of the code behind file. |
Debug | The Boolean value that enables or disables compilation with debug symbols. |
Description | The text description of the page, ignored by the parser. |
EnableSessionState | It enables, disables, or makes session state read-only. |
EnableViewState | The Boolean value that enables or disables view state across page requests. |
ErrorPage | URL for redirection if an unhandled page exception occurs. |
Inherits | The name of the code behind or other class. |
Language | The programming language for code. |
Src | The file name of the code behind class. |
Trace | It enables or disables tracing. |
TraceMode | It indicates how trace messages are displayed, and sorted by time or category. |
Transaction | It indicates 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
PreviousPageType指令爲頁分配一個類,以便頁是強類型的。
PreviousPagetype指令示例的基本語法爲:
<%@ PreviousPageType attribute="value"[attribute="value" ...] %>
The Reference Directive
Reference指令指示應編譯另一頁或用戶控制項並將其連結到當前頁。
Reference指令的基本語法是:
<%@ Reference Page ="somepage.aspx" %>
The Register Directive
Register派生用於註冊自定義伺服器控制項和用戶控制項。
Register指令的基本語法是: