-->

26 February 2020

ASP.NET–Directives

  Asp.Net CS By Example       26 February 2020

 ASP.NET–Directives 

 In this post, we are exploring more about ASP.NET – Directives. There are many directives that we can use within our ASP.NET pages.The directives section is one of the most important parts of an ASP.NET page. Directives control how a page is compiled, specify how a page is cached by web browsers, helps debugging (error-fixing), and allow we to import classes to usewithin our page's code.

ASP.NET directives can appear anywhere on a page, but they are commonly included at its very beginning. ASP.NET directives are instructions to specify optional settings, such as registering a custom control and page language. These settings describe how the web forms (.aspx) or user controls (.ascx) pages are processed by the .Net framework.

 A directive always begins with the special characters <%@ and ends with the characters %>. Directives are used primarily to provide the compiler with the information it needs to compile the page.

The syntax for declaring a directive is:
 <%@ directive_name attribute=value [attribute=value] %> 

 we use most of these directives throughout the Development.

1) The Application Directive :
  The Application directive defines application-specific attributes. It is provided at the top of the global.aspx file.

The basic syntax of Application directive is:
 <%@ Application Language="C#" %> 

The attributes of the Application directive are:

Sr.No Attributes Tag Description
1) Inherits The name of the class from which to inherit.
2) Description The text description of the application. Parsers and compilers ignore this.
3) Language The language used in code blocks.

2) The Assembly Directive :
  The Assembly directive links an assembly to the page or the application at parse time. This could appear either in the global.aspx file for application-wide linking, in the page file, a user control file for linking to a page or user control.

The basic syntax of Assembly directive is:
 <%@ Assembly Name ="myassembly" %> 

The attributes of the Assembly directive are:

Sr.No Attributes Tag Description
1) Name The name of the assembly to be linked.
2) Src The path to the source file to be linked and compiled dynamically.

3) The Control Directive :
 The control directive is used with the user controls and appears in the user control (.ascx) files.

The basic syntax of Control directive is:
 <%@ Control Language="C#" EnableViewState="false" %> 

The attributes of the Control directive are:

Sr.No Attributes Description
1) AutoEventWireup The Boolean value that enables or disables automatic association of events to handlers.
2) ClassName The file name for the control.
3) Debug The Boolean value that enables or disables compiling with debug symbols.
4) Description The text description of the control page, ignored by compiler.
5) EnableViewState The Boolean value that indicates whether view state is maintained across page requests.
6) Explicit For VB language, tells the compiler to use option explicit mode.
7) Inherits The class from which the control page inherits.
8) Language The language for code and script.
9) Src The filename for the code-behind class.
10) Strict For VB language, tells the compiler to use the option strict mode.

4) The Implements Directive :
 The Implement directive indicates that the web page, master page or user control page must implement the specified .Net framework interface.

The basic syntax of Implements directive is:
 <%@ Implements Interface="interface_name" %> 

5) The Import Directive :
 The Import directive imports a namespace into a web page, user control page of application. If the Import directive is specified in the global.asax file, then it is applied to the entire application. If it is in a page of user control page, then it is applied to that page or control.

The basic syntax of Import directive is:
 <%@ namespace="System.Drawing" %> 

6) The Master Directive :
  The Master directive specifies a page file as being the mater page.

The basic syntax of Master directive is:
 <%@ MasterPage Language="C#" AutoEventWireup="true" 
 CodeFile="SiteMater.master.cs" Inherits="SiteMaster" %> 

 This directive automatically casts the value of the Master property to the type of the Master Page.

7) The MasterType Directive :
  The MasterType directive assigns a class name to the Master property of a page, to make it strongly typed.

The basic syntax of MasterType directive is:
 <%@ MasterType attribute="value"[attribute="value" ...] %> 

8) The OutputCache Directive :
  We enable Page Output Caching by adding an <%@ OutputCache %> directive to a page. The OutputCache directive controls the output caching policies of a web page or a user control.

The basic syntax of OutputCache directive is:
 <%@ OutputCache Duration="15" VaryByParam="None" %> 

 The page also includes an <%@ OutputCache %> directive. If we refresh the page multiple times, we notice that the time is not updated until at least 15 seconds have passed.

We can assign two special values to the VaryByParam attribute:

Sr.No Attributes Description
1) none Causes any query string or form parameters to be ignored. Only one version of the page is cached.
2) * Causes a new cached version of the page to be created whenever there is a change in any query string or form parameter passed to the page.

9) The Page Directive :
 The Page directive defines the attributes specific to the page file for the page parser and the compiler.

The basic syntax of Page directive is:
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
Trace="true" %> 

The attributes of the Page directive are:

Sr.No Attributes Description
1) AutoEventWireup The Boolean value that enables or disables page events that are being automatically bound to methods; for example, Page_Load.
2) Buffer The Boolean value that enables or disables HTTP response buffering.
3) ClassName The class name for the page.
4) ClientTarget The browser for which the server controls should render content.
5) CodeFile The name of the code behind file.
6) Debug The Boolean value that enables or disables compilation with debug symbols.
7) Description The text description of the page, ignored by the parser.
8) EnableSessionState It enables, disables, or makes session state read-only.
9) EnableViewState The Boolean value that enables or disables view state across page requests.
10) ErrorPage URL for redirection if an unhandled page exception occurs.
11) Inherits The name of the code behind or other class.
12) Src The file name of the code behind class.
13) Language The programming language for code.
14) Trace It enables or disables tracing.
15) TraceMode It indicates how trace messages are displayed, and sorted by time or category.
16) Transaction It indicates if transactions are supported.
17) ValidateRequest The Boolean value that indicates whether all input data is validated against a hardcoded list of values.

10) The Register Directive :
 The Register derivative is used for registering the custom server controls and user controls.

The basic syntax of Register directive is:
 <%@ Register Src="~/footer.ascx" TagName="footer" TagPrefix="Tfooter" %> 

The attributes of the Register directive are:

Sr.No Attributes Description
1) TagPrefix Indicates the namespace that we want to associate with the User control for the current page. We can use any string that we want.
2) TagName Indicates the name that we want to associate with the User control for the current page. We can use any string that we want.
3) Src Indicates the virtual path to the User control (the path to the .ascx file).
logoblog

Thanks for reading ASP.NET–Directives

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.