Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Friday, 10 February 2012

Directives in ASPX FIles

Directives:
A directive is special instructions on how ASP.NET should process the page. The most common directive is <%@ Page %> which can specify many attributes used by the ASP.NET page parser and compiler.

Directives in ASP.NET control the settings and properties of page and user control compilers. They can be included anywhere on a page, although it is standard to place them at the beginning. Directives are used in both .aspx files (ASP.NET pages) and .ascx files (user control pages). ASP.NET pages actually support eight different directives.

    * @ Page
    * @ Control
    * @ Import
    * @ Implements
    * @ Register
    * @ Assembly
    * @ OutputCache
    * @ Reference



<%@ Page Language="C#" CodeFile="SampleCodeBehind.aspx.cs"
Inherits="Website.SampleCodeBehind"
AutoEventWireup="true" %>

The above tag is placed at the beginning of the ASPX file. The CodeFile property of the @ Page directive specifies the file (.cs or .vb or .fs) acting as the code-behind while the Inherits property specifies the Class from which the Page is derived. In this example, the @ Page directive is included in SampleCodeBehind.aspx, then SampleCodeBehind.aspx.cs.

Page directives are the most commonly used directives, and are used to edit a wide variety of settings that control how the page parser and page compiler work. The following is a list of some of the more commonly used page directive attributes in ASP.NET.

@ Page language="c#" Codebehind="WebForm1.aspx.cs"
         AutoEventWireup="false" Inherits="TestWebApp.WebForm1"

    * Language indicates the language in which the inline script code within the ASP.NET page is written (the code between <% %> tags). The value of this attribute can be C#, VB, or JS.
    * Codebehind indicates the name of the file being used as the code supporting this ASP.NET page. This file should reflect the Language setting;that is, if the language being used is C#, the CodeBehind file should have a .cs extension and be written in C#.
    * Inherits indicates a qualified class from which this ASP.NET page should inherit. Generally, this will be the name of the class described in the code-behind file.
    * AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired.AutoEventWireup is an attribute in Page directive. It's a Boolean attribute which indicates whether the asp.net pages events are auto-wired.

AutoEventWireup will have a value true or false. By default its value is set to false. We can specify the default value of the AutoEventWireup attribute in the following locations:
- The Machine.config file.
- The Web.config file.
- Individual Web Forms (.aspx files).
- Web User Controls (.ascx files)

In Machine.Config or WebConfig file this attribute can be declared as
<configuration>
<system.web> <pages autoEventWireup="true|false" /> </system.web>
</configuration>

As we know, when we change Machine.Config file, it will affect all asp.net webforms on the computer and if we change Web.config it will affect that application only. If you want this attribute change in a single webform change in page directive

Understanding its working AutoEventWireup is false when we create a new web application and event handlers are automatically created. We can find this in the InitializeComponent method
this.Load += new System.EventHandler(this.Page_Load);

Now declare a string public message in aspx.cs.
In aspx write <% Response.Write(message);%>. 
Give a string value for message

in page_load ( message="Hi. How you doing").
Run the application and you can see that you are getting above mentioned message. Now comment the event handler for page_load in aspx.cs (let the AutoEventWireup attribute in default mode (false) only).On running the application we will not get the message. Now with the event handler code for the Page_Load in the spx.cs file still commented; set theAutoEventWireup attribute to true in the .aspx page.On running the application this time, you will get the message. When AutoEventWireup is false the event handles are required for the page_load or page_init. When we set the value of the AutoEventWireup attribute to true, the ASP.NET
runtime does not require events to specify event handlers like Page_Load orPage_Init.

No comments:

Post a Comment