The @ Page Directive
The @ Page directive specifies the Web Form attributes that are enabled, disabled, or the specific values, such as the transaction attributes of a Web Form. The ASP.NET Framework uses these attributes to provide the specific capabilities of a Web Form when the page is rendered. This directive is also used by Visual Studio .NET to track related project files.
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 the class does not exist, then a system exception will be thrown. If you are using code embedded in <script runat=”server”></script< tages, the Inherits attribute is not required.
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.
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.
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.
Ok, in short, here it is.
- 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.