Different Types of directives in ASP.NET

 

In this article we discuss about various directives like Page, Control, Import, Implements, Register, Assembly, OutputCache, and Reference available in ASP.NET.

 

@Page: Defines page-specific attributes used by the ASP.NET page parser and compiler. It can be included only in .aspx file like below.

 

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

 

@Control: Defines control specific attributes used by the Asp.Net page parser and compiler. It can be included only in .ascx files as shown below.

 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

 

 

@Import: Explicitly imports a namespace into a page or user control. The Import directive cannot have more than one namespace attribute. To Import multiple namespaces use multiple @Import directives as shown below.

 

<%@ Import Namespace="System.Web" %> 

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

 

 

@Implements: It indicates that current page or user control implements the specific .NET framework interface as shown below.

 

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

 

 

@Register: Associates aliases with namespaces and class names for concise notation in custom server control syntax as shown below.

 

<%@ Register TagPrefix="web" TagName="cntrl" Src="~/WebUserControl.ascx" %>

 

 

@Assembly: Links an assembly to the current page during compilation, making all the assembly’s classes and interfaces available for use on the page like below.

 

<%@ Assembly Name="MyAssembly" %> 

<%@ Assembly Src="MyAssembly.cs" %>

 

 

@OutputCache: Declaratively controls the output caching policies of an Asp.Net page or a user control contained in a page as shown below to cache the particular page or user control.

 

<%@ OutputCache Duration="3000" VaryByParam="None" %>

 

 

@Reference: Declaratively indicates that another user control or page source file should be dynamically compiled and linked against the page in which this directive is declared.