
Converting Code-Behind Classes to Partial Classes
In Visual Studio 2005, Web application projects use partial classes to store designer-generated code. These classes are stored in a separate file from the code-behind file. By default, the Visual Studio 2005 Conversion Wizard does not create a *.designer.cs file or a *.designer.vb file for Web pages (.aspx files) or user controls (.ascx files). Instead, the code will look and work just like it did in Visual Studio .NET 2003.
Note |
|---|
| The Visual Studio 2005 Conversion Wizard will make the minimum number of changes to the code files during the conversion process. This helps ensure a smooth conversion to a Web application project in Visual Studio 2005. |
You can keep your code in the Visual Studio .NET 2003 format. If you do, you must manually update the control field declarations in the code-behind files like you did in Visual Studio .NET 2003. However, to take advantage of the ability to maintain field declarations in generated code, you should update your pages and controls to use the partial-class model introduced in ASP.NET 2.0. Partial classes make it easier to organize the generated code and custom code for your code-behind files. For more information, see Partial (Visual Basic) and partial (C# Reference).
To convert your code to use the partial-class model
-
Make sure your code compiles without errors.
-
In Solution Explorer, right-click the project name and click Convert to Web Application.This command iterates through each page and user control in the project, moves all control declarations to a .designer.cs file, and adds event handler declarations to the server-control markup in the .aspx and .ascx files.
-
When the process has finished, check the Task List window to see whether any conversion errors are reported. If the Task List displays errors, right-click the relevant page in Solution Explorer and select View Code and View Code Gen File to examine the code and fix problems.
Note |
|---|
| Errors and warnings that appear in the Task List window persist between Visual Studio sessions. After you have fixed errors listed in the window, you can clear items from the task list. |
-
Recompile your project to make sure that it compiles without errors.
Another way to convert your code to use the partial-class model is to use the Convert to Web Application command on individual pages. You might do this first on a few pages so you can see the changes that were made for each page before applying the changes to the whole application.
The Convert to Web Application command cannot be undone in Visual Studio. The best method for restoring the changes is to restore from a backup of the Visual Studio .NET 2003 project, and then to re-run the Visual Studio 2005 conversion as described earlier.
From this point, when you add new pages to the Web project, the pages will use the partial-class template by default. For more information, see ASP.NET 2.0 Migration Overview.