Article ID: 312311 - View products that this article applies to.
This article was previously published under Q312311
For a Microsoft Visual C# .NET version of this article, see 308143
This article demonstrates how to develop .aspx pages that use code-behind class files in Microsoft ASP.NET applications. The code samples in this article include the requirements for both code-behind class files that are precompiled and code-behind class files that are compiled on demand. For more information about code-behind class files and their deployment, see the "References" section.
RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
Create an ASP.NET Web application by using Microsoft Visual Basic .NETThis section demonstrates how to create a new ASP.NET Web application that is named CodeBehindSamples.
Use code-behind class filesIf you use code-behind class files with .aspx pages, you can separate the presentation code from the core application logic (or code-behind). The code-behind class file is compiled so that it can be created and used as an object. This allows access to its properties, its methods, its and event handlers. For this to work, the .aspx page must specify to inherit from the code-behind base class. To do this, use the Inherits attribute for the @ Page directive. The .aspx page inherits from the code-behind class, and the code-behind class inherits from the Page class.
By default, if you are using Visual Studio .NET, a Codebehind attribute is added to the @ Page directive. The .NET Framework does not actually use this attribute. Instead, Visual Studio .NET uses this attribute to maintain a reference to the associated code-behind file for the .aspx page.
To demonstrate how Visual Studio .NET uses the Codebehind attribute, remove the Codebehind attribute. Note that you can no longer right-click the .aspx page and then click View Code. This behavior occurs because Visual Studio .NET no longer contains a reference for the class file that it can use for the page. Remember that this is not how the .NET Framework uses code-behind class files, but how Visual Studio .NET manages these project files.
Use the Inherits attribute with precompiled classesIf you precompile your code-behind classes into an assembly, you can use the Inherits attribute to specify the class from which to inherit. In this scenario, you do not have to include the actual code-behind class file when you deploy the application. Instead, you must deploy the assembly and the .aspx page. You must put the assembly in the Bin folder for the application when you deploy the application.
This section demonstrates how to create a new Web Form that uses the precompiled approach and inherits from the code-behind class.
Use the Src attribute and compile on demandIf your code-behind class files will be compiled on demand instead of precompiled, you must use the Src attribute to specify the relative path of the code-behind class file. Make sure that you include the actual class file when you use this method to deploy the application.
Note If you develop your applications in Visual Studio .NET, see the "References" section in this article for more information about potential issues with using the Src attribute. Visual Studio .NET is designed to take advantage of precompiling your application code into an assembly instead of using the compile on demand approach that is described in this section.
For more information about assemblies, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/hk5f40ct(vs.71).aspxFor more information about deploying .NET Framework applications, visit the following MSDN Web site:
http://msdn.microsoft.com/en-us/library/6hbb4k3e(VS.80).aspxFor more information about articles, samples, and other resources that are related to ASP.NET programming,visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/aa286485.aspxFor more information about the @ Page directive and its various attributes, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ydy4x04a(vs.71).aspxFor more information about general Web Forms syntax, visit the following MSDN Web site:
http://msdn.microsoft.com/en-us/library/fy30at8h(VS.71).aspxFor more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/303247/ )ASP.NET code-behind model overview
(http://support.microsoft.com/kb/305141/ )ASP.NET page framework overview
(http://support.microsoft.com/kb/301392/ )How to create a virtual folder (Subweb) in IIS 4.0 or IIS 5.0
Article ID: 312311 - Last Review: May 11, 2007 - Revision: 2.8
Contact us for more help