Microsoft KB Archive/308143

= HOW TO: Work with Code-Behind Class Files in an ASP.NET Application by Using Visual C# .NET =

Article ID: 308143

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q308143





For a Microsoft Visual Basic .NET version of this article, see 312311.

IN THIS TASK
 SUMMARY Requirements Create an ASP.NET Web Application by Using Microsoft Visual C# .NET Use Code-Behind Class Files 

Use the Inherits Attribute and Precompiled Classes

 

Use the Src Attribute and Compile On Demand

 Troubleshooting</li> REFERENCES</li></ul>

<div class="summary_section">

SUMMARY
This article describes 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 of this article.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
 * Microsoft .NET Framework 1.0 or Microsoft .NET Framework 1.1
 * Microsoft Internet Information Services (IIS) version 5.0 or later

back to the top

Create an ASP.NET Web Application by Using Microsoft Visual C# .NET
This section demonstrates how to create a new ASP.NET Web application that is named CodeBehindSamples.
 * 1) Start Microsoft Visual Studio .NET.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Type, click Visual C# Projects. Under Templates, click ASP.NET Web Application.
 * 4) In the Name box, type CodeBehindSamples . In the Location box, type the server name. If you are using the local server, leave the location as http://localhost.

back to the top

Use Code-Behind Class Files
If 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, and its 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 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.

back to the top

Use the Inherits Attribute with Precompiled Classes
If 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. <ol> To add a new Web Form that is named InheritSample.aspx to your Visual Studio .NET project, follow these steps: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click the project node, click Add, and then click Add Web Form.</li> In the Name box, type InheritSample.aspx, and then click Open.</li></ol> </li> Switch to Design view, and then add a Web Form Label control to the .aspx page.</li> Right-click the .aspx page, and then click View Code. The code-behind file opens in the editor.</li>  In the code-behind file, add the following code to the Page_Load event handler private void Page_Load(object sender, System.EventArgs e) { Label1.Text = &quot;(Precompiled): Page_Load fired!&quot;; } Note This code only demonstrates that the code-behind class is involved in the sample at run time in the later steps. </li> Switch from the code-behind class file to the .aspx page in the editor, and then switch to HTML view.</li>  At the top of the page, review the code for the @ Page directive. The code should be similar to the following default code: <%@ Page language=&quot;c#&quot; Codebehind=&quot;InheritSample.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;CodeBehindSamples.InheritSample&quot; %> In this example, note that the .aspx page inherits from the code-behind class that is named InheritSamples in the CodeBehindSamples namespace. By default, a Web application that is created in Visual Studio .NET uses a. structure for the Inherits attribute value. </li> On the File menu, click Save All to save the Web Form and other associated project files.</li> In the Visual Studio .NET IDE, on the Build menu, click Build to build the project.</li> On the Project menu, click Show All Files.</li> In Solution Explorer, click to expand the Bin folder. The assembly that is generated when you compile the project from the previous section (which is CodeBehindSamples.dll in this example) appears in the Bin folder.</li> In Visual Studio .NET, right-click the page in Solution Explorer, and then click View in Browser to run the code. The label is populated with the following value:

<pre class="fixed_text">(Precompiled): Page_Load fired!

</li></ol>

back to the top

Use the Src Attribute and Compile On Demand
If 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 are developing 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. <ol> To add a new Web Form that is named SrcSample.aspx to your project in Visual Studio .NET, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In Solution Explorer, right-click the project node, click Add, and then click Add Web Form.</li> <li>In the Name box, type SrcSample.aspx, and then click Open.</li></ol> </li> <li>Switch to Design view, and then add a Web Form Label control to the .aspx page.</li> <li>Right-click the .aspx page, and then click View Code. The code-behind file opens in the editor.</li> <li> In the code-behind file, add the following code to the Page_Load event: private void Page_Load(object sender, System.EventArgs e) { Label1.Text = &quot;(Src): Page_Load fired!&quot;; }                   </li> <li>Switch from the code-behind class file to the .aspx page in the editor, and then switch to HTML view.</li> <li> At the top of the page, review the code for the @ Page directive. The code should be similar to the following default code: <%@ Page language=&quot;c#&quot; Codebehind=&quot;SrcSample.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;CodeBehindSamples.SrcSample&quot;%> </li> <li>To simplify this example, delete the Global.asax file from your project. This is only done in this example to prevent additional errors that are related to the code-behind page of the Global.asax file.</li> <li>On the File menu, click Save All to save the Web Form and other associated project files.

Note Because you want the code-behind class file for this sample to compile on demand, do not build the solution now.</li> <li>If you followed the steps that are listed in the &quot;Use the Inherits Attribute and Precompiled Classes&quot; section, you must delete the assembly in the Bin directory of the application before you follow the rest of the steps in this section. For more information, visit the Troubleshooting section of this article.</li> <li>To run the page, start Microsoft Internet Explorer and then manually enter the URL of the page. Do not select the View in Browser or the Browse With options from the Visual Studio .NET IDE. Otherwise, if you are using Visual Studio .NET 2003, the code-behind page will be precompiled into an assembly that is located in the Bin directory by default. After viewing the page, you will receive an error message that is similar to the following:

Could not load type 'CodeBehindSamples.SrcSample'.

This error occurs because the code-behind class file is not yet compiled, and you have not yet included the Src attribute to reference the code-behind class file.</li> <li> Add the Src attribute to the @ Page directive as follows: <%@ Page language=&quot;c#&quot; Codebehind=&quot;SrcSample.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;CodeBehindSamples.SrcSample&quot; Src=&quot;SrcSample.aspx.cs&quot;%> Note that the Src attribute is listed with the relative path of the code-behind class file (SrcSample.aspx.cs), and that the Inherits attribute value is set to reference CodeBehindSamples.SrcSample. </li> <li>On the File menu, click Save All to save the Web Form and other associated project files. Remember, do not build the solution because you want the code-behind class file for this sample to be compiled on demand.</li> <li>To run the page, start Internet Explorer and then manually enter the URL of the page. Do not select the View in Browser or the Browse With options from the Visual Studio .NET IDE. Otherwise, if you are using Visual Studio .NET 2003, the code-behind page will be precompiled into an assembly that is located in the Bin directory by default. At this point, the page should be loaded in the browser, and the label is populated with the following value:

<pre class="fixed_text">(Src): Page_Load fired!

The code-behind class file has now been compiled on demand and functions correctly.</li></ol>

back to the top

Troubleshooting
<ul> <li>You may receive an error message that is similar to the following if you precompile your application in Visual Studio .NET and then try to apply the compile on demand approach by using the Src attribute:

Compiler Error Message: CS1595: '. ' is defined in multiple places; using definition from '%windir%:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\ \d1444413\36fce407\assembly\dl2\009389be\231afa2d_d586c301\ .DLL'

Keep in mind that the directory names following  in the path that is listed in the error message will probably be different because ASP.NET automatically handles the building of the directories and their names.

If you use the Src attribute, you must follow these steps to resolve the issue that is associated with the error message: <ol> <li>You must delete the  directory that is referenced in the error message. You may also have to run the iisreset command from the command prompt before you complete this step. Otherwise, you may receive the following error message:

Cannot delete .dll: Access is denied.

Make sure the disk is not full or write-protected and that the file is not currently in use.

</li> <li>You must delete the assembly in the Bin directory of the application.</li> <li>To run the page, start Internet Explorer and then manually enter the URL to the page. Do not select the View in Browser or the Browse With options from the Visual Studio .NET IDE. Otherwise, if you use Visual Studio .NET 2003, the code-behind page will be precompiled into an assembly that is located in the Bin directory by default.</li></ol>

Note Microsoft recommends that you use the precompiled approach instead of the compile on demand method (by using the Src attribute) if you develop your applications with Visual Studio .NET to avoid these types of issues.</li> <li>If you want to or if you must contain your Web Form pages in a single file, develop your .aspx pages to contain your code instead of the code-behind class file. For more information about how to develop single-file Web Forms in Visual Studio .NET, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vstchWorkingWithSingle-FileWebFormsPagesInVisualStudio.asp?frame=true

</li> <li>If you do not precompile the code-behind class file and if you do not add the Src attribute to the @ Page directive, or if the virtual path for the attribute is not correct, you will receive the following error message:

Could not load type 'CodeBehindSamples.SrcSample'.

</li> <li>When you deploy .aspx pages, if their associated code-behind class files are precompiled, you only have to deploy the assembly to the Bin folder of the application. You do not have to deploy the actual code-behind class files with the application.</li> <li>When you deploy .aspx pages, if their associated code-behind class files are not precompiled, you must deploy the code-behind class files with the application. Additionally, you must add the Src attribute to the @ Page directive because the class file must be available when it is compiled on demand.</li></ul>

back to the top

<div class="references_section">