Thursday 28 February 2013

ASP.NET @ Page directive


Every ASP.NET generally begins with the @ Page directive. This defines page-specific attributes used by the ASP.NET page parser and compiler and can be included only in .aspx files. By default, Visual Studio creates a page directive as shown below (when you use the default web application template):
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

This directive can be used only in Web Forms pages. You can include only one @ Page directive per .aspx file. Further, you can define only one Language attribute per @ Page directive, because only one language can be used per page.The Page directive is made of many attributes. Let’s review a few of the important ones:

Title
Specifies a title for the page that is rendered within the HTML <title> tags in the response. The title can also be accessed programmatically as a property of the page.

Language
Specifies the language used when compiling all inline rendering (<% %> and <%= %>) and code declaration blocks within the page. Values can represent any .NET Framework-supported language, including Visual Basic, C#, or JScript. Only one language can be used and specified per page.

MasterPageFile
Sets the path to the master page for the content page or nested master page. Supports relative and absolute paths.

AutoEventWireup
Indicates whether the page's events are autowired. true if event autowiring is enabled; otherwise, false.

CodeBehind
Specifies the name of the compiled file that contains the class associated with the page. This attribute is not used at run time. This attribute is used for Web application projects. The CodeFile attribute is used for Web site projects.

Inherits
Defines a code-behind class for the page to inherit. This can be any class derived from the Page class. This attribute is used with the CodeFile attribute, which contains the path to the source file for the code-behind class. The Inherits attribute is case-sensitive when using C# as the page language, and case-insensitive when using Visual Basic as the page language.

If the Inherits attribute does not contain a namespace, ASP.NET checks whether the ClassName attribute contains a namespace. If so, ASP.NET attempts to load the class referenced in the Inherits attribute using the namespace of the ClassName attribute. (This assumes that the Inherits attribute and the ClassName attribute both use the same namespace.)

In addition to the attributes generated by default, you should also know about the following properties. They are a favorite of interviewers (they are either obscure, or ASP.NET 4 specific or they just like it).

ClientIDMode
Specifies the algorithm to use to generate ClientID values for controls. The default value is Predictable. The default value for controls is Inherit. Therefore, the default algorithm for controls in a page is determined by the ClientID setting of the page. A different default value can be set in the pages element of the Web.config file. For more information about the algorithms, see the ClientIDMode class.

ContentType
Defines the HTTP content type of the response as a standard MIME type. Supports any valid HTTP content-type string. 

EnableEventValidation
Enables validation of events in postback and callback scenarios. true if events are being validated; otherwise, false. The default is true.

Page event validation reduces the risk of unauthorized postback requests and callbacks. When the enableEventValidation property is set to true, ASP.NET allows only the events that can be raised on the control during a postback request or callback. With this model, a control registers its events during rendering and then validates the events during the post-back or callback handling. All event-driven controls in ASP.NET use this feature by default.

It is strongly recommended that you do not disable event validation. Before disabling event validation, you should be sure that no postback could be constructed that would have an unintended effect on your application.

EnableSessionState
Defines session-state requirements for the page. true if session state is enabled; ReadOnly if session state can be read but not changed; otherwise, false. The default is true. These values are case-insensitive. 

EnableViewState
Specifies whether view state is maintained across page requests. This value is true if view state is maintained, or false if view state is not maintained. The default is true.

No comments:

Post a Comment