Commands supported by ASP. NET pages and ASP. NET page commands

Source: Internet
Author: User
Tags net serialization

Commands supported by ASP. NET pages and ASP. NET page commands

Page processing instructions

The processing of page commands is used to configure the runtime environment for executing the page. In ASP. NET, commands can be located anywhere on the page, but it is good and common to place them at the beginning of a file. In addition, the name of the page instruction is case-insensitive, and the attribute value of the instruction does not need to be enclosed in quotation marks. @ Page is the most important and commonly used command in ASP. NET. Table 3.4 lists all ASP. NET commands. [101 ~ 102]


Table 3.4 commands supported by ASP. NET pages

Orders

Description

@ Assembly

Used to link an assembly to the current page or user control

@ Control

Defines attributes that are unique to a control. These attributes control the behavior of the control compiler.

@ Implements

Indicates that the current page or the current user control implements the specified. NET Framework interface.

@ Import

It is used to explicitly import a namespace to a page or user control.

@ Master

Used to identify the current page as an ASP. NET master page (see Chapter 6th ). ASP. NET 1.x does not support this command

@ MasterType

When you access the ASP. NET Master page through the Master property, it provides a method for creating a strong type reference for the Master page (see Chapter 6th ). ASP. NET 1.x does not support this command

@ OutputCache

Used to control the output cache policies of pages or user controls (see chapter 16th)

@ Page

Defines the unique properties of a page to control the actions of the page editor and language parser that process the page.

@ PreviousPageType

The PreviousPage attribute allows you to access previous pages by using the PreviousPage attribute.

@ Reference

Used to link a page or user control to the current page or user control.

@ Register

Create a custom tag in a page or control. The new tag (including the prefix and name) associates the namespace with the user-defined control.


 

All commands except @ Page, @ PreviousPageType, @ Master, @ MasterType, and @ Control can be declared on the Page and Control. @ Page and @ Control are mutually exclusive: @ Page can only be used in the. aspx file, while the @ Control command can only be used in the. ascx file. @ Master is used to define a very special page-master page ).

The syntax for processing commands is special, but the types of all commands are consistent. Multiple Attributes must be separated by spaces. The equal signs (=) between attributes and values cannot be blank, as shown below:

<% @ Directive_Name attribute = "value" [attribute = "value "... ] %> [103]

Each Command has its own type of attribute set. If you assign an error type value to an attribute or use an incorrect attribute in an attribute, compilation errors will occur.

 

Key point: the attribute content of a command is always parsed in plain text. However, some attributes should contain values that can be parsed into specific. NET Framework types. All command attributes when the ASP. NET page is parsed
Is extracted and stored in a dictionary. The attribute name and number must match the corresponding mode of the command. As long as the attribute value represented by a string can be converted to the target type, it is valid. For example, if an attribute only accepts values of the Boolean type, only true and false are valid.


@ Page command

The @ Page command can only be used on the. aspx Page. If it is used in other ASP. NET pages (such as controls and Web Services), compilation errors may occur. Each. aspx file can contain up to one @ Page command. From the perspective of syntax, it is not necessary to forcibly specify this command, but almost all complex pages actually do this.

The @ Page command has 30 attributes, which can be logically divided into three types: compilation (see table 3.5) and overall Page behavior (see table 3.6) and page output (see table 3.7 ). Each ASP. NET page is compiled at the first request. The HTML actually sent to the browser is generated by the class method, and these classes are also dynamically generated. The attributes listed in Table 3.5 are used to adjust the compiler parameters and select the language to use.


Table 3.5 @ Page directive attribute for Page Compilation

Genus

Description

ClassName

Specifies the name of the dynamically generated class when the page is requested. It must be a class name without namespace Information

CodeFile

Shows the path of the hidden class on the current page. This type of file must be deployed on the Web server. ASP. NET 1.x does not support this attribute

CodeBehind

The attribute used by Visual Studio. NET 2003 to indicate the path of the hidden class on the current page code. This type of file will be compiled into a deployable assembly (CodeFile attribute should be used for ASP. NET of Version 2.0 or later)

CodeFileBaseClass

Specifies the base class name of the page and its associated code hidden class. This attribute is optional, but must be specified if the CodeFile attribute is used. ASP. NET 1.x does not support this attribute

CompilationMode

Indicates whether the current page is compiled at runtime. ASP. NET 1.x does not support this attribute

CompilerOptions

Compile a series of compiler command line parameters on the current page

Debug

A boolean value that indicates whether the page should be compiled using debugging symbols

Explicit

A boolean value used to determine whether to set the Option Explicit mode of Visual Basic to On when compiling the page. Option Explicit forces programmers to explicitly declare all variables. If the language selected on the current page is not Visual Basic. NET, this attribute is ignored.

Inherits

Defines the base class to be inherited from the current Page. It can be any class derived from the Page class.

Language

Used to prompt Inline code blocks during compilation (<%... The language used by the code in the <script> section. Supported languages include Visual Basic. NET, C #, JScript. NET, and J #. If this parameter is not specified, Visual Basic. NET is set by default.


 

Continue table

 

Genus

Description

LinePragmas

Used to determine whether line pragma should be generated in the source code during runtime)

MasterPageFile

Indicates the master page of the current page. ASP. NET 1.x does not support this attribute

Src

Indicates the path of the source file that contains the base class specified by the implementation of Inherits. This attribute is not used by Visual Studio and other Rapid Application Development (RAD) designers.

Strict

A boolean value used to determine whether to set the Option Strict of Visual Basic to On when compiling the current page. If Option Strict is enabled, only type-safe conversions are allowed. Implicit conversions that may cause data loss are prohibited (in this case, the behavior is the same as that of C ). If the language selected on the current page is not Visual Basic. NET, this attribute is ignored.

Trace

A boolean value that indicates whether the tracking function is enabled. If the tracking function is enabled, additional information is appended to the output of the page. The default value is false.

TraceMode

When the tracking function is enabled, a message display method is displayed on the current page. Valid values: SortByTime and SortByCategory. If the tracking function is enabled, the default value of this attribute is SortByTime.

WarningLevel

Indicates the warning level of the compiler. The compilation process stops when the specified level is reached. Valid value: 0 ~ 4


 

[104 ~ 105]

Note that the default values of the Explicit and Strict attributes are read from the application configuration file. You can combine all computer-level, application-level, and Folder-level settings to obtain ASP. NET application settings. This shows that we can also control the default values of the Explicit and Strict attributes. If you do not change the default configuration information (that is, the configuration information created when the. NET Framework is installed), the Explicit and Strict parameters are set to true by default. If you delete all settings related to the configuration file, the value of both settings will change to false.

You can use the attributes listed in table 3.6 to control the overall page behavior and supported functions to some extent. For example, you can set custom error pages, disable session statuses, and control page transaction processing behaviors.


Table 3.6 attributes of the @ Page command used to control Page Behaviors

Genus

Description

AspCompat

Boolean attribute. If it is set to true, the current page is allowed to be executed on a single-thread unit (STA) thread. This setting enables the page to call COM + 1.0 components, and components that need to access unmanaged ASP built-in objects developed using Visual Basic 6.0 (this topic will be discussed in chapter 14th)

Async

If this parameter is set to true, the generated page class is derived from IHttpAsyncHandler, instead of enabling IHttpHandler to add some built-in non-synchronous functions to the page. ASP. NET 1.x does not support this attribute


 

Continue table

 

Genus

Description

AsyncTimeOut

Defines the timeout time (in seconds) used to process asynchronous tasks ). The default value is 45 seconds. ASP. NET 1.x does not support this attribute

AutoEventWireup

Boolean attribute, used to indicate whether to enable page events. The default value is true. Pages developed using Visual Studio. NET will set this attribute to false, and page events will be bound to the handler respectively.

Buffer

Boolean attribute used to determine whether HTTP Response buffering is enabled. The default value is true.

Description

Provides the text description of the current page. The ASP. NET page parser ignores this attribute and only serves as a document description.

EnableEventValidation

Boolean attribute is used to determine whether to generate an implicit field on the current page and buffer the input fields that support event data verification. The default value is true. ASP. NET 1.x does not support this attribute

EnableSessionState

Defines how the current page processes session data. If it is set to true, you can read/write the session status. If set to false, the application cannot use session data. Finally, if it is set to ReadOnly, the session data can only be read, but cannot be changed.

EnableViewState

Boolean attribute used to indicate whether a view is maintained between page requests. View status is the context of the page call and is used to save the set of current Page Status values during the round-trip process (Chapter 15th will explain this)

EnableTheming

A boolean attribute that indicates whether the current page applies a topic to an embedded control. The default value is true. ASP. NET 1.x does not support this attribute

EnableViewStateMac

Boolean attribute used to indicate whether ASP. NET generates an authentication code for a specific computer and appends it to the view status of the page (except Base64 encoding ). Mac in the attribute name represents "machine authentication check (MAC ). If this attribute is set to true, ASP. NET checks the authentication code of the view status when sending a response to ensure that it is not tampered with on the client.

ErrorPage

Define a target URL, which points to a prompt page. When an unhandled page exception occurs, the user is redirected to this location.

MaintainScrollPositionOnPostback

Indicates whether to restore the client browser's scroll bar position after sending back. The default value is fasle.

SmartNavigation

A boolean attribute that indicates whether the current page supports smart navigation of Internet Explorer 5 or later. Intelligent Navigation makes the page refresh without losing the scrolling position and element focus

Theme, StyleSheetTheme

Specifies the name of the selected topic (or style sheet topic) for the current page. ASP. NET 1.x does not support this attribute


 

Continue table

 

Genus

Description

Transaction

Indicates whether the current page supports or requires transactions. Valid values: Disabled, NotSupported, Supported, Required, and RequiresNew. By default, transaction support is disabled.

ValidateRequest

Boolean attribute used to indicate whether request verification is performed. If set to true, ASP. NET checks all input data based on a list of hard-coded potential dangerous values. This feature helps reduce the risk of cross-site scripting attacks on pages. The default value is true. ASP. NET 1.0 does not support this function

ViewStateEncryptionMode

The encryption method used to indicate the view status. There are three possible enumerated values: Auto, Always, and Never. The default value is Auto, indicating that the view State is encrypted only when the control requires it. Note that the view data is encrypted for each page Processing request, causing additional overhead on the server.


 

[106 ~ 107]

The attributes listed in table 3.7 are used to control the output information format generated on the page. For example, we can set the content type of the page or make the output localized as much as possible.


Table 3.7 controls the output @ Page Directive attributes

Genus

Description

ClientTarget

Indicates the browser to be oriented when the ASP. NET Server Control presents its content

CodePage

Indicates the value of the corresponding code page. This attribute is set only when a page is created using a code page (instead of the default code page of the Web server that runs the page. In this case, set this attribute to the code page of the development computer. A code page is a collection of symbols, including numbers, punctuation marks, and other symbols. The code pages in different languages are different.

ContentType

Defines the response content type as a standard MIME type. Supports any valid HTTP Content string

Culture

It is used to indicate the regional settings of the current page. Regional information includes the writing and sorting system, calendar, and currency format. The property value must be a regional name, that is, it must contain language and country information. For example, en-US is a valid value, and en itself is considered unrelated to the country.

LCID

A 32-bit value that defines the page's regional identifier. By default, ASP. NET uses the Web server's region settings.

ResponseEncoding

Indicates the encoding scheme name of the current page. This value is used to set the CharSet attribute of the HTTP header of the content type. Internally, ASP. NET processes all strings according to Unicode

Title

Used to indicate the title of the current page. This is not useful for general pages, because HTML <title> labels can be used, this attribute is defined to help developers not access <title> on the Content Page (depending on the Structure Design of the master page) and set the title for it.


 

Continue table

 

Genus

Description

UICulture

It is used to specify the default region information name used by the Resource Manager to search for regional-specific resources at runtime.


 

It is not hard to see that many attributes in table 3.7 are related to page localization. ASP. NET (more generally. NET Framework) greatly simplifies the task of building multilingual international applications. Chapter 2 will discuss this issue in depth.


@ Assembly command

@ Assembly command is used to link an Assembly to the current page so that its classes and interfaces can be used on this page. When ASP. NET compiles this page, several default assemblies will be linked in. Therefore, this command should be used only when a non-default Assembly needs to be linked. Table 3.8 lists the. NET Assembly automatically provided to the compiler. [108]


Table 3.8 default linked assembly

Assembly file name

Description

Mscorlib. dll

Provides core. NET Framework functions, including type, AppDomain, and runtime services.

System. dll

Provides another set of system services, including regular expressions, compilation, local methods, file input/output, and network

System. Configuration. dll

Defines the class for reading/writing configuration file data. ASP. NET 1.x does not contain this Assembly

System. Data. dll

Defines data containers and data containers, including the entire ADO. NET Framework.

System. Drawing. dll

Implemented the GDI + Function

System. EnterpriseServices. dll

Provides classes that allow interaction between components of the service and COM +.

System. Web. dll

This Assembly implements ASP. NET core services, controls, and classes

System. Web. Mobile. dll

This Assembly implements ASP. NET core mobile services, controls, and classes .. . NET Framework Version 1.0 does not contain this Assembly during installation.

System. Web. Services. dll

Contains core code that drives Web Service Running

System. Xml. dll

Implemented the XML function of. NET Framework.

System. Runtime. Serialization. dll

API that defines the. NET serialization function. In ASP. NET 2.0 applications, this Assembly is one of the most common assembly that developers can add. This Assembly was not introduced in versions earlier than ASP. NET 3.5.

System. ServiceModel. dll

Defines the classes and structures of the Windows Communication Foundation (WCF) service. This Assembly was not introduced in versions earlier than ASP. NET 3.5.

System. ServiceModel. Web. dll

Defines additional classes required for ASP. NET and AJAX. This Assembly was not introduced in versions earlier than ASP. NET 3.5.

System. WorkflowServices. dll

Defines the classes required by workflows and WCF services. This Assembly was not introduced in versions earlier than ASP. NET 3.5.


 

In addition to these assemblies, the ASP. NET runtime automatically links the assemblies in the Bin subdirectory of the Web application to all pages. By editing settings in the computer-level web. config file, you can change, extend, or limit the default assembly list. This change applies to all ASP. NET applications running on the Web server. In addition, you can edit the application-specific web. config file to customize the Assembly List of each application one by one. To prevent all the assemblies in the Bin directory from being linked to the page, you can delete the following code from the root configuration file:

<Add assembly = "*"/> 109]

 

Warning: for ASP. NET applications, the entire configuration property set is set at the computer level. Initially, all applications running on the server computer share the same settings. Independent applications can rewrite some of these settings in their own web. config file. Each application can put a web. config file in its root directory, and put other specialized web. config copies under the subdirectory specified by the application. The setting of each page depends on all settings in all configuration files found from the computer level to the current folder path. In ASP. NET 1. x, the machine. config file contains the complete default setting tree. In ASP. NET 2.0, settings related to Web applications are moved to the web. config file, which is in the same system folder as machine. config. This folder is named CONFIG and is located in the ASP. NET installation path: % WINDOWS % \ Microsoft. Net \ Framework \ [version].

 

To link the required assembly to the page, use the following syntax:

<% @ Assembly Name = "AssemblyName" %>

<% @ Assembly Src = "assembly_code.cs" %>

@ Assembly command supports two mutex attributes: Name and Src. Name indicates the Name of the assembly to be linked. The name cannot contain the path and extension. Src indicates the path of the source file to be dynamically compiled and linked to the page. @ Assembly command can appear multiple times in the main part of the page. In fact, for each assembly to be linked, you need to add commands separately. Name and Src cannot be used in a @ Assembly command at the same time, but different commands defined on the page can be selected separately.

 

Note: Although Name points to an existing assembly to be loaded, the performance difference between Name and Src is very small. Source files referenced by Src are compiled only once when they are requested for the first time. The ASP. NET Runtime Library maps the source file to the dynamically compiled assembly, and uses the compiled code until the source file is changed. That is to say, after the application-level first call, whether using Name or Src has the same impact on page performance.


@ Import command

The @ Import command is used to link a specified namespace to a page so that all defined types can be accessed on the page without using a fully qualified name ). For example, to create an ADO. NET DataSet class instance, you can import the System. Data namespace or use a fully qualified name like the following:

System. Data. DataSet ds = new System. Data. DataSet (); [110]

Once the System. Data namespace is introduced to the current page, we can write code more naturally as follows:

DataSet ds = new DataSet ();

@ Import command syntax is self-described:

<% @ Import namespace = "value" %>

@ Import can be used multiple times in the page body. The @ Import command of ASP. NET is equivalent to the using Statement of C # And the Import Statement of Visual Basic. NET. Looking back at the unmanaged C/C ++, we can say that this directive plays almost the same role as the # include Directive.

 

Note: @ Import only helps the compiler parse the class name and does not automatically link the required assembly. Using the @ Import command can shorten the class name. However, if the Assembly containing the class is not properly linked, a type error occurs in the compiler. If the Assembly has not been linked, using "fully qualified class names" will not help, because the compiler requires a type definition. You may have noticed that the names of the assembly and namespace are often the same. But remember, this is a coincidence that the Assembly and namespace are completely different entities and require corresponding commands respectively.

 

For example, to connect to the SQL Server database and obtain some unconnected data, we need to import the following two namespaces:

<% @ Import namespace = "System. Data" %>

<% @ Import namespace = "System. Data. SqlClient" %>

We need the System. Data namespace to use the DataSet and DataTable classes, and use the System. Data. SqlClient namespace to prepare and send commands. In this case, we do not need to link the Assembly, because the System. Data. dll assembly is linked by default.


@ Implements command

The @ Implements command is used to indicate specific interfaces in the. NET Framework implemented on the current page. An interface is a set of logically related function signatures and serves as a contract for each component to expose its function set. Unlike abstract class, interfaces do not provide code or execution functions. To implement an interface on the ASP. NET page, define the required methods and attributes in the <script> section. The syntax of the @ Implements command is as follows:

<% @ Implements interface = "InterfaceName" %> [111]

If you need to implement multiple interfaces on the page, the @ Implements command can appear multiple times on the page. Note: If you decide to define the logic of all pages in a separate file, you must not use this command to implement the interface, but in the Code hiding class.


@ Reference command

The @ Reference command is used to create a dynamic link between the current page and a specified page or user control. This function plays a major role in cross-page communication. We can use it to create a strong-type instance of a user control. Let's take a look at its syntax.

The @ Reference command can appear on the Page multiple times and has two mutex attributes: Page and Control. Both attributes are used to specify the path of the source file:

<% @ Reference page = "source_page" %>

<% @ Reference control = "source_user_control" %>

The Page attribute is used to point to a. aspx source file, while the Control attribute contains the path of the. ascx user Control. In both cases, the referenced source files will be dynamically compiled into the Assembly, so that the classes defined in the source files can be used in the active reference page during programming. At runtime, the ASP. NET page is an instance of the. NET Framework class with a specific interface consisting of methods and attributes. When the page is actively referenced for execution, the referenced page becomes a class that represents the. aspx source file, and can be instantiated and encoded as needed. Note: To make this command take effect, the referenced page must be in a domain with the master page. Cross-Site calls are not allowed, and the Page and Control attributes only accept relative virtual paths.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.