PAGE DIRECTIVES. Page Directives They are instructions, inserted at the top of an ASP.NET page, to...

19
PAGE DIRECTIVES

Transcript of PAGE DIRECTIVES. Page Directives They are instructions, inserted at the top of an ASP.NET page, to...

Page 1: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

PAGE DIRECTIVES

Page 2: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

Page Directives

They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.

So it is type of mixed settings related to how a page should render and processed.

There are 11 types of page directives in ASP.NET.

When used, directives can be located anywhere in an .aspx or .ascx file, though standard practice is to include them at the beginning of the file.

Each directive can contain one or more attributes(paired with values) that are specific to that directive.

Page 3: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@Page Directive

It enables you to specify attributes and values for an ASP.NET page to be used when the page is parsed and compiled.

Every .aspx files should include this @Page directive to execute.

<%@Page Language=“VB” AutoEventWireUp=“true” CodeFile=“Sample.aspx.vb” Inherits=“Sample” Title=“Sample Page Title”%>

Page 4: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

Attribute Values

AutoEventWireUp The Boolean value that enables or disables Page events that are being automatically bound to methods; for example, Page_Load

Buffer The Boolean value that enables or disables HTTP response buffering

ClassName Class name for the page CodeFile Name of the code behind file Debug The Boolean value that enables or disables

compilation with debug symbols EnableSessionState Enables, disables or makes session state

read-only EnableViewState The Boolean value that enables or disables

view state across page requests ErrorPage URL for redirection if an unhandled page

exception occurs

Page 5: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

Attribute Values

Inherits The name of the code behind or other class

Language Programming language for code

Src File name of the code behind class

Trace Enables or disables tracing

TraceMode Indicates how trace messages are to be displayed - sorted by time or

category

Page 6: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@Master Directive

The @Master Directive belongs to Master Pages i.e. .master files.

The master page will be used in conjunction of any number of content pages.

So the content pages can inherit the attributes of the master page.

The @Master Directive has only fewer attributes as follows :

• Language – This attribute tells the complier about the language being used in the code – behind.

• AutoEventWireUp – For every page there is an automatic way to bind the events to methods in the same master file or in code behind. The default value is true.

• CodeFile – Specifies the code-behind file with which the Master page is associated.

Page 7: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

• Title – Sets the Master Page title.

• MasterPageFile – Specifies the location of the Master Page file to be used with the current Master Page. This is called Nested Master Page.

• EnableViewState

• Inherits

<%@Master Language=“VB” AutoEventWireUp=“true” CodeFile=“Sample.aspx.vb” Inherits=“Sample”%>

Page 8: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@Control Directive

It is used when we build an ASP.NET user controls.

The @Control Directive helps us 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.

<%@Control Language=“VB” AutoEventWireUp=“true” CodeFile=“MyControl.ascx.vb” Inherits=“MyControl”%>

Page 9: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

Attribute Values

AutoEventWireUp The Boolean value that enables or disables automatic association of events to handlers

ClassName File name for the control Debug The Boolean value that enables or disables

compilation with debug symbolsEnableViewState The Boolean value that enables or disables

view state across page requests Explicit For VB language, tells the compiler to use

Option Explicit mode Inherits The class from which the control page

inherits Language Language for code and script Src File name of the code behind class Strict For VB language, tells the compiler to use

the Option Strict mode

Page 10: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@Register Directive

The @Register Directive associates aliases with namespaces and class names for notation in custom server control syntax.

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

The main attributes of @Register Directive are :

• 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.

Page 11: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

• TagPrefix – The alias to relate to the namespace.

<%@Register TagName=“MyUserControl” TagPrefix=“uc” Src=“~\usercontrol\MyUserControl.ascx”%>

Page 12: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@Reference Directive The @Reference Directive declares that another ASP.NET page or user control should be compiled along with the current page or user control.

The main attributes of @Reference Directive are :

• Control – User Control that ASP.NET should dynamically compile and link to the current page at run time.

• Page – The Web Form Page that ASP.NET page should dynamically compile and link to the current page at run time.

• VirtualPath – Specifies the location of the page or user control from which the active page will be referenced.

<%@Reference VirtualPath=“MyReferencePage.ascx”%>

Page 13: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@PreviousPageType Directive It is a new directive that makes excellence in asp.net pages.

The concept of cross-page posting between ASP.NET pages is achieved by this directive.

This directive is used to specify the page from which the cross-page posting initiates.

• 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.

<%@PreviousPageType VirtualPath=“~/MypreviousPage.aspx”%>

Page 14: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@OutputCache Directive It controls the output caching policies of the ASP.NET page or user control.

You can even cache programmatically through code by using VB.NET or Visual C#.NET

The main attributes of @OutputCache Directive are :

• Duration – The duration of time in seconds that the page or user control is cached.

• Location – To specify the location to store the output cache. To store the output cache on the browser client where the request originated set the value as “client”. To store the output cache on the web server, mention the location as “server”.

• VaryByParam – List of strings used to vary the output cache, separated with semi-colon.

Page 15: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

• VaryByControl – List of strings used to vary the output cache of a user control, separated with semi-colon.

• VaryByCustom – String of values, specifies the custom output caching requirements.

• VaryByHeader – List of HTTP headers used to vary the output cache, separated with semi-colon.

<%@OutputCache Duration=“60” Location=“server” VaryByParam=“None”%>

Page 16: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@Import Directive

It allows you to specify any namespaces to be imported to the ASP.NET pages or user controls.

By importing, all the classes and interfaces of the namespace are made available to the page or user control.

<%@Import namespace=“System.Data”%>

<%@Import namespace=“System.Data.SqlClient”%>

Page 17: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@Implements Directive

The @Implements Directive gets the ASP.NET page to implement a specified .NET framework interface.

The only single attribute is Interface, helps to specify 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.

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

Page 18: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@Assembly Directive

The @Assembly Directive is used to make your ASP.NET page aware of external components.

This directive supports two attributes :-

• Name – Enables you to specify the name of an assembly you want to attach to the page. Here you should mention the filename without the extension.

• Src – Represents the name of a source code file.

<%@Assembly Name=“MyAssemblyName”%>

Page 19: PAGE DIRECTIVES. Page Directives  They are instructions, inserted at the top of an ASP.NET page, to control the behavior of ASP.NET pages.  So it is.

@MasterType Directive To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @MasterType directive.

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

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

If you have public properties defined in a master page that you’d like to access in a strongly-typed manner. You can add the MasterType directive into a page as shown :

<%@MasterType VirtualPath=“MasterPage.master”%>

this.Master.HeaderText = “Label updated using MasterType directive with VirtualPath attribute”