Sunday 22 November 2015

Asp.Net Page Directives

Introduction:

Here we are going to discuss about asp.net page directives here I try to explain about different page directive of asp.net page.

Explanation:

 ASP.NET directives are something that is a part of every ASP.NET page. You can control the behavior of your ASP.NET pages by using these directives. Here’s an example of the Page directive:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
    Inherits="_Default" %>

Eleven directives are at your disposal in your ASP.NET pages or user controls. You use these directives in your applications whether the page uses the code-behind model or  the inline coding model.
Basically, these directives are commands that the 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] %>


From this, you can see that a directive is opened with a <%@ and closed with a %>. It is best to put these directives at the top of your pages or controls because this is traditionally where developers expect to see them (although the page still compiles if the directives are located at a different place). Of course, you can also add more than a single attribute to your directive statements, as shown in the following:
<%@ [Directive] [Attribute=Value] [Attribute=Value] %>
The following table describes the directives at your disposal in ASP.NET.

 Directive
Description
Assembly
Links an assembly to the Page or user control for which it is associated.
Control
Page directive meant for use with user controls (.ascx).
Implements
Implements a specified .NET Framework interface.
Import
Imports specified namespaces into the Page or user control.
Master
Enables you to specify master page–specific attributes and values to use when the page parses or compiles. This directive can be used only with master pages (.master).
MasterType
Associates a class name to a Page in order to get at strongly typed references or members contained within the specified master page.
OutputCache
Controls the output caching policies of a Page or user control.
Page
Enables you to specify page specific attributes and values to use when the page parses or compiles. This directive can be used only with ASP.NET pages (.aspx).
PreviousPageType
Enables an ASP.NET page to work with a postback from another page in the application.
Reference
Links a Page or user control to the current Page or user control.
Register
Associates aliases with namespaces and class names for notation in custom server control syntax.
 
The following sections provide a quick review of each of these directives.

@Page


The @Page directive enables you to specify attributes and values for an ASP.NET page (.aspx) to be used when the page is parsed or compiled.

This is the most frequently used directive of the bunch. Because the ASP.NET page is such an important part of ASP.NET, you have quite a few attributes at your disposal. The following table summarizes the attributes available through the @Page directive.
 
Attribute
Description
AspCompat
Permits the page to be executed on a single-threaded apartment thread when given a value of True. The default setting for this attribute is False.
Async
Specifies whether the ASP.NET page is processed synchronously or asynchronously.
AutoEventWireup
Specifies whether the page events are autowired when set to True. The default setting for this attribute is True.
Buffer
Enables HTTP response buffering when set to True. The default setting for this attribute is True.
ClassName
Specifies the name of the class that is bound to the page when the page is compiled.
CodeFile
References the code-behind file with which the page is associated.
CodePage
Indicates the code page value for the response.
CompilerOptions
Compiler string that indicates compilation options for the page.
CompileWith
Takes a String value that points to the code-behind file used.
ContentType
Defines the HTTP content type of the response as a standard MIME type.
Culture
Specifies the culture setting of the page. ASP.NET now includes the capability to give the Culture attribute a value of Auto to enable automatic detection of the culture required.
Debug
Compiles the page with debug symbols in place when set to True.
Description
Provides a text description of the page. The ASP.NET parser ignores this attribute and its assigned value.
EnableSessionState
Session state for the page is enabled when set to True. The default setting is True.
EnableTheming
Page is enabled to use theming when set to True. The default setting for this attribute is Trues.
EnableViewState
View state is maintained across the page when set to True. The default value is True.
EnableViewStateMac
Page runs a machine-authentication check on the page’s view state when the page is posted back from the user when set to True. The default value is False.
ErrorPage
Specifies a URL to post to for all unhandled page exceptions.
Explicit
Visual Basic Explicit option is enabled when set to True. The default setting is False.
Language
Defines the language being used for any inline rendering and script blocks.
LCID
Defines the locale identifier for the Web Form’s page.
LinePragmas
Boolean value that specifies whether line pragmas are used with the resulting assembly.
MasterPageFile
Takes a String value that points to the location of the master page used with the page. This attribute is used with content pages.
MaintainScrollPositionOnPostback
Takes a Boolean value, which indicates whether the page should be positioned exactly in the same scroll position or if the page should be regenerated in the uppermost position for when the page is posted back to itself.
PersonalizationProvider
Takes a String value that specifies the name of the personalization provider used in applying personalization to the page.
ResponseEncoding
Specifies the response encoding of the page content.
SmartNavigation
Specifies whether to activate the ASP.NET Smart Navigation feature for richer browsers. This returns the postback to the current position on the page. The default value is False.
Src
Points to the source file of the class used for the code behind of the page being rendered.
Strict
Compiles the page using the Visual Basic Strict mode when set to True. The default setting is False.
Theme
Applies the specified theme to the page using the ASP.NET themes feature.
Title
Applies a page’s title. This is an attribute mainly meant for content pages that must apply a page title other than what is specified in the master page.
Trace
Page tracing is enabled when set to True. The default setting is False.
TraceMode
Specifies how the trace messages are displayed when tracing is enabled. The settings for this attribute include Sort-ByTime or SortByCategory. The default setting is SortByTime.
Transaction
Specifies whether transactions are supported on the page. The settings for this attribute are Disabled, NotSupported, Supported, Required, and RequiresNew. The default setting is Disabled.
UICulture
The value of the UICulture attribute specifies what UI Culture to use for the ASP.NET page. ASP.NET  now includes the capability to give the UICulture attribute a value of Auto to enable automatic detection of the UICulture.
ValidateRequest
When this attribute is set to True, the form input values are checked against a list of potentially dangerous values. This helps protect your Web application from harmful attacks such as JavaScript attacks. The default value is True.
WarningLevel
Specifies the compiler warning level at which to stop compilation of the page. Possible values are 0 through 4.
 
Here is an example of how to use the @Page directive:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
    Inherits="_Default" %>

@Master

The @Master directive is quite similar to the @Page directive except that the @Master directive is meant for master pages (.master). In using the @Master directive, you specify properties of the templated page that you will be using in conjunction with any number of content pages on your site.

Any content pages (built using the @Page directive) can then inherit from the master page all the master content (defined in the master page using the @Master directive). Although they are similar, the @Master directive has fewer attributes available to it than does the @Page directive. The available attributes for the @Master directive are shown in the following table.
 
 Attribute
Description
AutoEventWireup
Specifies whether the master page’s events are autowired when set to True. Default setting is True.
ClassName
Specifies the name of the class that is bound to the master page when compiled.
CodeFile
References the code-behind file with which the page is associated.
CompilerOptions
Compiler string that indicates compilation options for the master page.
CompileWith
Takes a String value that points to the code-behind file used for the master page.
Debug
Compiles the master page with debug symbols in place when set to True.
Description
Provides a text description of the master page. The ASP.NET parser ignores this attribute and its assigned value.
EnableTheming
Indicates the master page is enabled to use theming when set to True. The default setting for this attribute is True.
EnableViewState
Maintains view state for the master page when set to True. The default value is True.
Explicit
Indicates that the Visual Basic Explicit option is enabled when set to True. The default setting is False.
Inherits
Specifies the CodeBehind class for the master page to inherit.
Language
Defines the language that is being used for any inline rendering and script blocks.
LinePragmas
Boolean value that specifies whether line pragmas are used with the resulting assembly.
MasterPageFile
Takes a String value that points to the location of the master page used with the master page. It is possible to have a master page use another master page, which creates a nested master page.
Src
Points to the source file of the class used for the code behind of the master page being rendered.
Strict
Compiles the master page using the Visual Basic Strict mode when set to True. The default setting is False.
WarningLevel
Specifies the compiler warning level at which you want to abort compilation of the page. Possible values are from 0 to 4.
 
Here is an example of how to use the @Master directive:

<%@ Master Language="VB" CodeFile="MasterPage1.master.vb"
    AutoEventWireup="false" Inherits="MasterPage" %>

@Control

The @Control directive is similar to the @Page directive except that @Control is used when you build an ASP.NET user control. The @Control directive allows you to define the properties to be inherited by the user control.

These values are assigned to the user control as the page is parsed and compiled. The available attributes are fewer than those of the @Page directive, but quite a few of them allow for the modifications you need when building user controls. The following table details the available attributes.
 
Attribute
Description
AutoEventWireup
Specifies whether the user control’s events are auto wired when set to True. Default setting is True.
ClassName
Specifies the name of the class that is bound to the user control when the page is compiled.
CodeFile
References the code-behind file with which the user control is associated.
CompilerOptions
Compiler string that indicates compilation options for the user control.
CompileWith
Takes a String value that points to the code-behind file used for the user control.
Debug
Compiles the user control with debug symbols in place when set to True.
Description
Provides a text description of the user control. The ASP.NET parser ignores this attribute and its assigned value.
EnableTheming
User control is enabled to use theming when set to True. The default setting for this attribute is True.
EnableViewState
View state is maintained for the user control when set to True. The default value is True.
Explicit
Visual Basic Explicit option is enabled when set to True. The default setting is False.
Inherits
Specifies the CodeBehind class for the user control to inherit.
Language
Defines the language used for any inline rendering and script blocks.
LinePragmas
Boolean value that specifies whether line pragmas are used with the resulting assembly.
Src
Points to the source file of the class used for the code behind of the user control being rendered.
Strict
Compiles the user control using the Visual Basic Strict mode when set to True. The default setting is False.
WarningLevel
Specifies the compiler warning level at which to stop compilation of the user control. Possible values are 0 through 4.
 
 The @Control directive is meant to be used with an ASP.NET user control. The following is an example of how to use the directive:

<%@ Control Language="VB" Explicit="True"
    CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl"
    Description="This is the registration user control." %>

@Import

The @Import directive allows you to specify a namespace to be 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 user control. This directive supports only a single attribute: Namespace.
The Namespace attribute takes a String value that specifies the namespace to be imported. The @Import directive cannot contain more than one attribute/value pair. Because of this, you must place multiple namespace imports in multiple lines as shown in the following example:

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

Several assemblies are already being referenced by your application. You can find a list of these imported namespaces by looking in the web.config.comments file found at C:\Windows\Microsoft.NET\ Framework\v2.0xxxxx\CONFIG. You can find this list of assemblies being referenced from the <assem-blies> child element of the <compilation> element. The settings in the web.config.comments file are as follows:

<assemblies>
   <add assembly="mscorlib" />
   <add assembly="System, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089" />
   <add assembly="System.Configuration, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
   <add assembly="System.Web, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
   <add assembly="System.Data, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089" />
   <add assembly="System.Web.Services, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
   <add assembly="System.Xml, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089" />
   <add assembly="System.Drawing, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
   <add assembly="System.EnterpriseServices, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
   <add assembly="System.Web.Mobile, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
   <add assembly="*" />
</assemblies>

Because of this reference in the web.config.comments file, these assemblies need not be referenced in a References folder, as you would have done in ASP.NET 1.0/1.1. You can actually add or delete assemblies that are referenced from this list. For example, if you have a custom assembly referenced continuously by each and every application on the server, you can simply add a similar reference to your custom assembly next to these others. Note that you can perform this same task through the web.config file of your application as well.

Even though assemblies might be referenced, you must still import the namespaces of these assemblies into your pages. The same web.config.comments file contains a list of namespaces automatically imported into each and every page of your application. This is specified through the <namespaces> child element of the <pages> element.

<namespaces>
   <add namespace="System" />
   <add namespace="System.Collections" />
   <add namespace="System.Collections.Specialized" />
   <add namespace="System.Configuration" />
   <add namespace="System.Text" />
   <add namespace="System.Text.RegularExpressions" />
   <add namespace="System.Web" />
   <add namespace="System.Web.Caching" />
   <add namespace="System.Web.SessionState" />
   <add namespace="System.Web.Security" />
   <add namespace="System.Web.Profile" />
   <add namespace="System.Web.UI" />
   <add namespace="System.Web.UI.WebControls" />
   <add namespace="System.Web.UI.WebControls.WebParts" />
   <add namespace="System.Web.UI.HtmlControls" />
</namespaces>

From this XML list, you can see that quite a number of namespaces are imported into each and every one of your ASP.NET pages. Again, you can feel free to modify this selection in the web.config.comments file or even make a similar selection of namespaces from within your application’s web.config file.
For instance, you can import your own namespace in the web.config file of your application in order to make the namespace available on every page where it is utilized.

<?xml version="1.0"?>
<configuration>
   <system.web>
      <pages>
         <namespaces>
            <add namespace="MyCompany.Utilities" />
         </namespaces>
      </pages>
   </system.web>
</configuration>

Remember that importing a namespace into your ASP.NET page or user control gives you the opportunity to use the classes without fully identifying the class name. For example, by importing the namespace System.Data.OleDb into the ASP.NET page, you can refer to classes within this namespace by using the singular class name (OleDbConnection instead of System.Data.OleDb.OleDbConnection). 

@Implements

The @Implements directive gets the ASP.NET page to implement a specified .NET Framework interface. This directive supports only a single attribute: Interface.

The Interface attribute directly specifies the .NET Framework interface. When the ASP.NET page or user control implements an interface, it has direct access to all its events, methods, and properties.

Here is an example of the @Implements directive:

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

@Register

The @Register directive associates aliases with namespaces and class names for notation in custom server control syntax. You can see the use of the @Register directive when you drag and drop a user control onto any of your .aspx pages. Dragging a user control onto the .aspx page causes Visual Studio 2005 to create an @Register directive at the top of the page.

This registers your user control on the page so that the control can then be accessed on the .aspx page by a specific name.

The @Register directive supports five attributes, as described in the following table.


Attribute
Description
Assembly
The assembly you are associating with the TagPrefix.
Namespace
The namespace to relate with TagPrefix.
Src
The location of the user control.
TagName
The alias to relate to the class name.
TagPrefix
The alias to relate to the namespace.


Here’s an example of how to use the @Register directive to import a user control to an ASP.NET page:

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

@Assembly

The @Assembly directive attaches assemblies, the building blocks of .NET applications, to an ASP.NET page or user control as it compiles, thereby making all the assembly’s classes and interfaces available to the page. This directive supports two attributes: Name and Src.
Name: Enables you to specify the name of an assembly used to attach to the page files. The name of the assembly should include the file name only, not the file’s extension. For instance, if the file is MyAssembly.vb, the value of the name attribute should be MyAssembly.
Src: Enables you to specify the source of the assembly file to use in compilation.

The following provides some examples of how to use the @Assembly directive:

<%@ Assembly Name="MyAssembly" %>
<%@ Assembly Src="MyAssembly.vb" %>

@PreviousPageType

This directive is used to specify the page from which any cross-page postings originate.
The @PreviousPageType directive is a new directive that works with the new cross-page posting capability that ASP.NET 2.0 provides. This simple directive contains only two possible attributes: TypeName and VirtualPath:
•    TypeName: Sets the name of the derived class from which the postback will occur.
•    VirtualPath: Sets the location of the posting page from which the postback will occur.

@MasterType

The @MasterType directive associates a class name to an ASP.NET page in order to get at strongly typed references or members contained within the specified master page. This directive supports two attributes:

•    TypeName: Sets the name of the derived class from which to get strongly typed references or members.

•    VirtualPath: Sets the location of the page from which these strongly typed references and members will be retrieved.

<%@ MasterType VirtualPath="~/Wrox.master" %>

@OutputCache

The @OutputCache directive controls the output caching policies of an ASP.NET page or user control. This directive supports the ten attributes described in the following table. 

Attribute
Description
CacheProfile
Allows for a central way to manage an application’s cache profile. Use the CacheProfile attribute to specify the name of the cache profile detailed in the
web.config.
Duration The duration of time in seconds that the ASP.NET page or user control is cached.
Location
Location enumeration value. The default is Any. This is valid for .aspx pages only and does not work with user controls (.ascx). Other possible values include Client, Downstream, None, Server, and ServerAndClient.
NoStore
Specifies whether to send a no-store header with the page.
SqlDependency
Enables a particular page to use SQL Server cache invalidation - a new feature of ASP.NET 2.0.
VaryByControl
Semicolon-separated list of strings used to vary the output cache of a user control.
VaryByCustom
String specifying the custom output caching requirements.
VaryByHeader
Semicolon-separated list of HTTP headers used to vary the output cache.
VaryByParam
Semicolon-separated list of strings used to vary the output cache.

Here is an example of how to use the @OutputCache directive:

<%@ OutputCache Duration="180" VaryByParam="None" %>
Remember that the Duration attribute specifies the amount of time in seconds during which this page is to be stored in the system cache.
 
@Reference

The @Reference directive declares that another ASP.NET page or user control should be compiled along with the active page or control. This directive supports just a single attribute:
•    VirtualPath: Sets the location of the page or user control from which the active page will be referenced.
Here is an example of how to use the @Reference directive:

<%@ Reference VirtualPath="~/MyControl.ascx" %>

No comments:

Post a Comment