ASP.NET Directives

ASP.NET pages enable you to specify various options that can be used by compilers at the time of compilation. You can specify these commands and options with the help of directives . A directive name begins with the @ character and is enclosed in <% and %> tags. A directive can be placed anywhere in a Web page but is usually placed at the top. Directives contain one or more attribute/value pairs, which are not case sensitive and need not be placed in quotes.

Table 2.3 lists the various types of directives that can be added to an ASP.NET page.

Table 2.3. ASP.NET Page Directives

Directive

Description

Assembly

Links an assembly to the application or a Web page during compilation. It is similar to the /reference command-line switch of the C# compiler ( csc.exe ). You can have multiple Assembly directives on a page to link multiple assemblies.

Control

Specifies user control- related attributes that are used by the ASP.NET compiler when the user control (ASCX file) is compiled.

Implements

Indicates that the Web page (ASPX file) or the user control (ASCX file) implements an interface specified by the Interface attribute of the directive.

Import

Imports a namespace into a page, a user control, or an application. It is similar to the using directive in C#. You can have multiple Import directives to import different namespaces.

OutputCache

Controls the output caching of a Web page (ASPX file) or user control (ASCX file).

Page

Specifies page-related attributes used by the ASP.NET compiler when the Web client requests the ASPX page. See Table 2.4 for a list of attributes allowed in a Page directive.

Reference

Indicates a Web page or user control that should be compiled and linked during compilation of the ASPX page that contains the Reference directive. It can contain either the Page attribute (ASPX file name) or Control attribute (ASCX file name).

Register

Registers a custom server control or a user control to be used within an ASP.NET page.

The Page Directive

The Page directive is used to specify page-related attributes that control how an ASP.NET page is compiled and executed. An ASP.NET page can have only one Page directive. Also, the keyword Page is optional in a Page directive. Table 2.4 lists the various attributes of the Page directive.

Table 2.4. Attributes of the Page Directive

Attribute

Description

AspCompat

Indicates whether the page is to be executed on a Single-Threaded Apartment (STA) thread model for backward compatibility. The default value is false .

AutoEventWireUp

Indicates whether ASP.NET should automatically connect methods with specific names , such as Page_Init(), Page_Load() , and so on, with the page events. The default value is true . However, for Web pages created using Visual Studio .NET, the default value is false .

Buffer

Indicates whether the HTTP response buffer is enabled. The default value is true .

ClassName

Specifies the name for the dynamically generated class file for the ASPX page.

ClientTarget

Represents the target user agent (such as Mozilla/4.0) or alias (such as IE4) according to which the ASP.NET page should be rendered.

CodePage

Indicates the culture codepage value for the ASP.NET page. Supports any valid codepage value. For example, the value 932 specifies a Japanese codepage.

CompilerOptions

Indicates the compiler options and switches to be passed to the language compiler.

ContentType

Indicates the MIME type for the page response.

Culture

Indicates the culture setting for the page and supports any valid culture string, such as en-US, en-GB , and so on.

Debug

Indicates whether the compilation should include debug symbols. The default value is false .

Description

Represents the description of the page. The page compilers ignore this attribute.

EnableSessionState

Indicates whether the session state is enabled ( true ), read-only ( ReadOnly ), or disabled ( false ) for the page. The default value is true .

EnableViewState

Indicates whether the view state is maintained for the page and its container controls. The default value is true .

EnableViewStateMac

Indicates whether the view state should be verified against Machine Authentication Check (MAC) to ensure that the view state is not tampered with.

ErrorPage

Specifies the error page where the user is redirected whenever an unhandled exception occurs in the page.

Explicit

Ignored when the Language attribute is C# .

Inherits

Represents the name of the code-behind class that contains code for the ASPX page and from which the dynamically generated class inherits.

Language

Represents any .NET programming language used for inline coding in the page.

LCID

Defines the 32-bit locale identifier for code in the page.

ResponseEncoding

Defines the response encoding for the page, such as UTF7Encoding, UTF8Encoding, ASCIIEncoding, UnicodeEncoding , and so on.

Src

Represents the source filename of the code-behind class that is dynamically compiled when the Web page is requested . Visual Studio .NET does not use this attribute because it precompiles the code-behind class.

SmartNavigation

Indicates whether smart navigation is enabled for the page. The smart navigation feature preserves the scroll position and element focus whenever the page is refreshed. The default value is false .

Strict

Ignored when the Language attribute is C# .

Trace

Indicates whether page-level tracing is enabled. The default value is false .

TraceMode

Indicates how trace messages are to be displayed when tracing is enabled. The default value is SortByTime , and the other possible value is SortByCategory .

Transaction

Indicates whether transactions are supported on the page. The default value is Disabled , and the other possible values are NotSupported, Supported, Required , and RequiresNew .

UICulture

Indicates the culture setting for the user interface of a page. Supports any valid culture string, such as en-US, en-GB , and so on.

WarningLevel

Specifies the warning level at which page compilation should be stopped . The possible values are from to 4 .

The following example demonstrates how you can display information related to the execution of a page using the Trace attribute of the Page directive:

  1. Add a new text file to project 315C02 , and name the text file Example2_2.aspx .

  2. Switch to the HTML view and add the following code to the Example2_2.aspx file:

     <%@ Page Language="C#" Trace="true"%> <html> <body> Trace Information </body> </html> 
  3. View Example2_2.aspx in the browser. Trace information enables you to analyze the execution information for a Page request.

The process of displaying execution information for an ASP.NET page is also called tracing . Various sections in the tracing information display a wealth of information about the page execution.



MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
ISBN: 789729016
EAN: N/A
Year: 2005
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net