How to - Create a missing designer.cs file
I was running into this just now and found a quick way to solve it, so I just posted it for future reference.
I have an ASP.Net web application project in .Net 2.0 which was migrated from .Net 1.1 some time ago. I noticed that a few aspx pages did not have a designer.cs file. All controls on those pages were listed as protected variable in the main code behind page, like this example:
/// An example label
protected System.Web.UI.WebControls.Label Label2;
And it annoyed me. It was not consistent with the new pages in the application, and most other aspx pages that were also there during migration did have the designer.cs file. So how to solve this, in order to get a more consistent use of ASP.Net. I followed the following steps to do this:
- First step - Make sure the latest version of the project is on your machine.
- Second step is to remove the protected variables for the controls from the normal code behind cs file.
- Now right-Click on the project and select 'Convert to Web Application'
The missing designer.cs is now created and added to the project. The protected variables, which were removed from the main code behind class are now in the partial class in the newly created designer.cs. The class in the original code behind class is also changed to a partial class.
If you forget step 2 and still want to remove the protected variables from your code behind class, then follow the following steps:
- Remove the protected variables for the controls from the normal code behind cs file.
- Open the ASPX file and make sure you view it in Source (HTML) mode
- Select the entire HTML mark up using CTRL-A
- Now press CTRL-K followed by CTRL-F
The HTML mark-up will be re-aligned and the designer.cs file will be recreated, including all the protected variables.