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:

/// <summary>
/// An example label
/// </summary>
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:

  1. First step – Make sure the latest version of the project is on your machine.
  2. Second step is to remove the protected variables for the controls from the normal code behind cs file.
  3. 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:

  1. Remove the protected variables for the controls from the normal code behind cs file.
  2. Open the ASPX file and make sure you view it in Source (HTML) mode
  3. Select the entire HTML mark up using CTRL-A
  4. 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.

8 thoughts on “How to – Create a missing designer.cs file”

  1. Jan, for your second scenario you could do the following:

    1. Remove the protected variables for the controls from the normal code behind cs file.
    2. Delete the designer.cs file
    3. Right-click on the aspx file, and select Convert to Web Application.

    This technique also works for the dreaded “Unable to connect to undo manager of source file…”.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>