ASP.NET Directives

Discussion in 'ASP.NET' started by MinalS, Jul 25, 2015.

  1. The syntax for directives in ASP.NET is:

    Code:
    <%@ directive_name attribute = value [ attribute = value ] %>
    
    Every directive consist of attribute- value pair. There are various application directives supported in ASP.NET. The other directives used with the ASP.NET files are:
    1. @Import: It is used for importing the namespace in an application.
    2. @Assembly: The assembly is linked to the application at the parse time.
    In this tutorial, let’s explore all the directives used in ASP.NET.

    Application Directive



    The attributes specific to an application are defined using the application directive. They are present in the .aspx files.

    Syntax for application directive:
    Code:
    <%@ Application attribute="value" [ attribute = "value" ] %>
    
    The various attributes used by the application are:
    1. Language: The code blocks are used inside the language
    2. Inherits: The class name from where the code is inherited
    3. Description: The explanation of the application. They are not processed by the parsers and compilers.
    4. CodeBehind: The source code file containing the application class is defined.
    5. Language: The language used for compiling the application is defined.
    Example:
    Code:
    <%@ Application Inherits="MyApp1.object" Description="New App" %>
    

    Control Directive



    It is used for defining the user specific controls used by the compiler and parser.

    Syntax for control directive:
    Code:
    <%@ Control attribute="value" [ attribute = "value" ] %>
    
    Some of the attributes used by the control directive are:
    1. ClassName: The name of the file used in the control
    2. AutoEventWireup: It states the Boolean value used for enabling and disabling the event handlers
    3. Description: It states the description of the control page, ignored by the compiler
    4. Inherits: The class from where the control page is inherited
    5. Explicit: The option explicit mode is used by the user.
    6. EnableViewState: It is the Boolean value indicating the view state is maintained across page requests.
    7. Strict: The compiler is advised with the strict mode.
    8. Language: The code and script language is defined
    9. CodeFileBaseClass: The path of the base class and the corresponding code behind class is defined.
    10. EnableTheming: The themes used on the control are defined. It is represented by a Boolean value.
    11. Src: The path to the source file consisting of the code to be linked to the control.
    12. TargetSchema: The name of the schema used for validating the control is defined.
    Example:

    Code:
    
    <%@ Control Language="C#" EnableViewState="false" %>
    

    Assembly Directive



    The assembly directive is used for linking the application file. The files can be user control, web pages, or a application file. All the assembly classes and interfaces are present.

    The syntax for the assembly directive is:

    Code:
    
    <%@ Assembly Name="assembly1" %>
    <%@ Assembly Src="pathname" %>
    
    
    The attributes of the assembly directive are:
    1. Src: The path for the source code files to be linked dynamically
    2. Name: The assembly name to be linked.
    Example:
    Code:
    <%@ Assembly Name=AssemblyNew" %>
    <%@ Assembly Src="Demo.cs" %>
    

    Import Directive



    The namespace is imported to the web page using the import directive. The directive specified in the global.aspx file is assigned to the complete application. If it is specified to the specific page, it is applied to that control or page.

    The syntax for import directive is:

    Code:
    <%@ Import Namespace="value" %>
    
    The attribute used in the Import directive:
    1. namespace: The fully qualified name of the imported namespace.
    Example:

    Code:
    <%@ Import Namespace="System.Net" %>
    

    Implements Directive



    The implements directive states that the page, user control or master page is implemented in specified .NET framework.

    The syntax for implements directive is:

    Code:
    <%@ Implements interface ="interface_name" %>
    
    Attributes associated with the implements directive:
    1. interface: The interface implemented on the page or user control.

    MasterType Directive



    The directive assigns the class name to the Master property of a page. The page is made strongly typed.

    The syntax for the MasterType directive is:

    Code:
    <%@ MasterType attribute="value" [ attribute = "value" ] %>
    
    Attributes associated with the MasterType directive are:
    1. TypeName: States the name for the master page
    2. VirtualPath: The path assigned to the file creating a strong type

    Master Directive



    The directive is used to specify the page file as master file.

    The syntax for the master directive is:

    Code:
    <%@ MasterPage Language = "C#" AutoEventWireUp="true" CodeFile="SiteMaster.master.cs" Inherits="SiteMaster" %>
    
    Attributes associated with the Master directive are:
    1. ClassName: The name of the class associated with the control.
    2. CodeFile: The file containing the separate partial class and event handlers associated with it.
    3. AutoEventWireup: The simple event handlers used for specifying the life cycle of a web page.
    4. Debug: States to compile the master page using the debug symbols.
    5. EnableTheming: It shows the look of the master page and controls of the master page
    6. Description: A text description is provided to the user.
    7. Explicit: The page is compiled using the Option Explicit mode. The value true states that the compile is enabled.
    8. Language: The language used for the inline rendering. Code declaration blocks within the page.
    9. MasterPageFile: The .master file states the master page.
    10. Strict: The page is compiled using the Option Strict mode.
    11. Src: The source file name is defined in the code behind class.
    Example:

    Code:
    <%@ Master Language="C#" CodeFile="Master1.master.cs" Inherits="MasterPage" %>
    

    Page Directive



    The page directive defines the attributes for the specific page for the compiler or parser.

    Syntax for page directive:

    Code:
    <%@ Page attribute="value" [ attribute="value" …. ] %>
    
    The attributes of the page directive are:
    1. Buffer: It is a Boolean value stating the enabling and disabling of the HTTP response buffering.
    2. ClassName: It is the name of the class for the page.
    3. AutoEventWireup: It states that the page is automatically bound to the methods. Indicated by a Boolean value.
    4. CodeFile: The code behind file name
    5. ClientTarget: The browser where the controls are rendered by the user.
    6. Description: The text value described for the particular page. The parser does not pass the values
    7. EnableViewState: The value used for stating the enabled or disabled condition of the view state
    8. Error Page: The redirection of the URL if an unhandled exception occurs
    9. Language: The programming language for the code to be added
    10. TraceMode: It is used for enabling or disabling the tracing values.
    11. ValidateRequest: The value stating the input data is validated across all the listed values.
    12. Transaction: The transaction supported by the browser is defined.
    Example:
    Code:
    <%@Page Language="C#" EnableViewState="True" Description="Web Page" %>
    

    OutputCache Directive



    The directive controls the output caching capacity of the web page or the control.

    Syntax for OutputCache directive:

    Code:
    <%@ OutputCache Duration="Seconds" 
        Shared="True | False"
        VarByControl = "controlname"
        VarByHeader="header"
        VarByParam = "parameter"
        CacheProfile="profile name"
        ProviderName="provider name"
        Location=" Client | Server | None | ServerAndClient | Any "
    %>
    
    Attributes used in the OutputCache directive are:
    1. Duration: The time used by the page is cached. It is defined in seconds.
    2. VarByHeader: The list of the HTTP headers used by the output cache.
    3. VarByParam: The list used for modifying the output cache.
    4. VarByCustom: The custom cache output requirements are stated.
    Example:

    Code:
    <%@ OutputCache Duration="50" VarByParam="None" %>
    

    Reference: Directive



    The reference directive is used for showing that another page or control is compiled to the current page.

    Syntax for reference directive:

    Code:
    <%@ Reference Page="Default.aspx" %>
    
    Attributes of the Reference directive are:
    1. Page: The external page that needs to be dynamically linked to the current file.
    2. virtualpath: It states the reference of the file.
    3. Control: The user control used for compiling and linking to the current file.

    Register Directive



    It is used for referring the custom control to the tag prefix created by the user.

    The syntax for the Register directive is:

    Code:
    
    <%@ Register tagprefix="tag1"
        namespace="namespace"
        assembly="assembly" %>
    
    <%@ Register tagprefix="tag2"
        tagname="tagname"
        src="pathname" %>
    
    Attributes for the Register directive are:
    1. assembly: The assembly where the namespace is associated with the tagprefix attribute.
    2. src: The location where the user controls file is associated with the tagprefix:: tagname combination.
    3. tagname: The alias associated with the class.
    4. tagprefix: The alias providing the shorthand reference to the namespace of the file.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice