CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Friday, October 13, 2006

Page directive and its attributes

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>

Inherits

The Inherits attribute defines the code behind class that the Web Form inherits. This can be any class that derives from the System.Web.UI.Page class. The Inherits attribute is only necessary when using code behind. If you are using code embedded in <script runat="server"></script> tages, the Inherits attribute is not required.

Src

The Src attribute specifies the name of the code behind file (the file on the file system where the class code exists) to use when dynamically compile when the Web Form is requested. The ASP.NET Framework will find the class file, and use a Just-In-Time (JIT) compiler to complie the class.

The Src attribute and the Inherits attribute are not mutually exclusive - if you use the Src attribute you must still specify the class to inherit from in the Inherits attribute, even if the code behind file only has one class in it. The ASP.NET Framework will always look in the assembly for the class first. If the class exists in the compiled assembly, then it will be used. If the class is not in the assembly then the ASP.NET Framework will JIT compile the class from the Src file, ad use it.

Visual Studio .NET does not use the Src attribute. Rather, Visual Studio .NET expects that all Web Form code behind classes will be compiled into an assembly. The Build menu, Build option will create the assembly and put the DLL in the \bin directory.

Codebehind

This is the big confusing one...Codebehind. The Codebehind attribute is NOT an ASP.NET attribute, it is a Visual Studio .NET attribute. The only thing the ASP.NET Freamework knows about this attribute is that it should be ignored. Visual Studio .NET uses the Codebehind attribute to track the class file that is related to the Web Form. This enables Visual Studio .NET to automatically embedd code in the class file when the design environment chenges, such as when a new server control is placed on the Web Form.

One of the biggest misconceptions is that replacing the Codebehind attribute with the Src attribute will solve the problem when you get an error stating that the Inherits class can not be found. While this does remedy the problem, the belief of why is incorrect. Swapping Codebehind for Src just removes the ability for Visual Studio .NET to embed code in the related class file, and enables JIT compilation for the Web Form. Generally this error is the result of NOT using the Src attribute, and not building the assembly. To resolve this error, either build the assembly, or add the Src attribute.

If you are not using the Src attribute, you must rebuild the assembly for any class changes to take affect.

The advantage to using pre-compiled assemblies, and not the Src attribute is that the source code files do not have to be put on the Web server. All the class logic resides in the assembly, so only the DLL needs to be distributed, not the source code.

AutoEventWireUp

This is a freebie! The AutoEventWireUp attribute, set to true by default, specifies whether or not the Page events are auto-wired. In other words, when the value is true, the Page events are auto-wired, so that Page_Load(), Page_PreRender(), etc. event handlers work with no extra coding. When set to false you must explicitly create set the Page event handlers to methods with signatures that match the event signature.

Visual Studio .NET sets this value to false, and uses the Page_Init() event handler (which is always auto-wired) to invoke an InitializeComponent() method, where the Page event handlers get defind, as well as the event handlers for any server controls on the Web Form.

private void Page_Init(object sender, EventArgs e)
{
  InitializeComponent();
}

#region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

private void InitializeComponent()
{
  this.MyButton.Click += new System.EventHandler(this.MyButton_Click);
  this.Load += new System.EventHandler(this.MyPage_Load);

}
#endregion

In summary:

The Inherits attribute specifies the System.Web.UI.Page derived class to inherit from.

  • The Src attribute can be used to enable JIT compiling of the code behind class, but the ASP.NET Framework will always look in the compiled assembly for the class first. The Inherits attribute MUST be used with the Src attribute.
  • The Codebehind attribute is used by Visual Studio .NET to track the codebehind class file. This class is ignored by the ASP.NET Framework.
  • The AutoEventWireUp attribute specifies whether the Page event should be auto-wired or not.

Useful links

http://www.dotnetjunkies.com/Tutorial/

3623050C-B046-4E7B-A2C7-0934F1105312.dcik

 

Post a Comment