ASP.NET Page Directivies

 

ASP.NET page directives are something that is a part of every ASP.Net Page.

 

We can control the page’s behavior by using these directives. There are eleven directives which can be used  in Microsoft  ASP.NET pages or user control.

 

Basically these are commands that compiler uses when the page is compiled. Directives are simple to incorporate into your pages. A directive is written in the following format:

 

<%@[Directive][Attribute=Value] %>.So the directive is opened with <%@ and closed with %>.

 

We can add more than a single attribute to the directive statements.

 

<%@[Directive][Attribute=Value] [Attribute=Value]  %>.

 

Now I will explain the different directives used in Microsoft  ASP.NET page

 

@Page:

@Page Directive is most frequently used and most important in the bunch of directives.

 

This directive enables to specify the attributes of an ASP.Net Page(.aspx).This attributes are used when the page is complied and parsed.For this directives there are many attributes to be used.

 

Here I am explaining a few of the important attributes for this directive.

 

Ø  AsyncàSpecifies whether the ASP.NET page is processed synchronously or asynchronously.

Ø  AutoEventWireupàSpecifies whether page events are autowired when set to True.The default setting is True

Ø  ClassàSpecifie the class bound to this page when it is complied.

Ø  Compilation ModeàSpecifies whether the ASP.NET  should compile the page or not.Available options for this are Always(which is the default) and Auto(means If possible ASP.Net Page will not compile the page).

Ø  CultureàASP.Net 3.5 and 4 include the capability to give the Culture attribute a value of Auto to enable automatic detection of the Culture required.

Ø  EnableEventValidationàSpecifies whether enable validation of events in postback and callback scenarios. The default setting of True means events will be validated.

Ø  ErrorPageàSpecifies a URL to post for all unhandled exceptions.

Ø  MasterPageàAs the name suggests points to the location of MasterPage used within the page.

Ø  CodeFileàname of the code behind file.

Ø  Languageàdefines the language being used for any script or inline blocks.

 

Eg:- <%@Page Language="VB" AutoEventWireUp="False" codeFile="Default.aspx.vb"  inherits="_Default"%>

 

@Master

This directive is similar to @Page directive but it’s used for master page(.master) .By using a @Master directive,we can specify a template page that will be using with any number of content pages.This directive has fewer attributes available to it than @Page directive.

 

Eg:- <%@Master Language="VB" AutoEventWireUp="False" codeFile="MasterPage1.master.vb"  inherits="MasterPage"%>

 

@Control

This Directive is used with usercontrol pages(.ascx)

 

Eg:-<%@Control Language="VB" Explicit="true" codeFile="Webusercontrol.ascx.vb"  inherits="Webusercontrol"%>

 

@Import

The @Import directive allows to specify the namespace imported into the ASP.NET Page or user control.By importing all the classes and interfaces of the namespace are made available to the page or use control.

 

Eg:- <%@Import Namespace="System.Data"%>

 

@Implements

This directive gets the ASP.Net page to implement a specified .NET Famework interface.This supports only a single attribute:Interface

 

Eg:- <%@Implements Interface="System.Web.UI.IValidator"%>

 

@Register

This directive associates aliases with namespaces and class names for notation in custom server control syntax.

 

Eg:- <%@ Register TagPrefix="MyTag" Namespace="MyName.MyNamespace" Assembly="MyAssembly" %>

 

The attributes for this directive is the following;

TagPrefixàAlias to relate to the namespace.

TagNameàAlias relate to className.

SrcàLocation of user control.

NamespaceàNamespace relate with TagPrefix

Assemblyàassembly associating with TagPrefix

 

@Assembly

This directive links ans assembly to the page or usercontrol for which it is associated.

 

@MasterType

Associate a class name to a page to get a strongly type reference or members contained within the specified master Page.

 

@Outputcache

Controls the output caching policies of a page or use control

 

@PreviousPageType

Enables the ASP.Net page to work with a postback fro another page in the application

 

@Reference

This directive declares that another ASP.Net page or user control should be compiled along with the active page or control.