Tip: regenerate aspx.designer.cs files when corrupted

Tip: regenerate aspx.designer.cs files when corrupted


This post explains how you can regenerate the designer.cs and designer.vb files when they are corrupted, missing or giving compile errors.

Problem

Occasionally it happens that you receive compile errors in your *.aspx.designer.cs or *.aspx.designer.vb files in your project and it seems that there’s virtually nothing you can do. Some seem to be caused by Visual Studio bugs, others are caused by wrong usage or copy and pasting pages from outside your project. Be as it may, often these issues are deceptively easy to solve! Here’s a 1 minute quick solution if you consider yourself somewhat experienced. Need more guidance? Try the step-by-step solution.

Quick Solution

Just follow these three simple steps and your *.aspx.designer.cs will magically reappear! If this goes too quick, or you just feel you need a bit more guidance, try the step by step solution in the following section

  1. Locate the corrupted aspx.designer.cs file through the Solution Explorer
  2. Delete only the designer.cs file from your project
  3. Rightclick your main aspx file and select Convert to Web Application.

Step by step solution

Before you start, make sure that you actually have a problem. Don’t do something drastic when it is not necessary. Clean your project (Build > Clean Project) and the rebuild it (Build > Rebuild Project). If this does not solve your problem, and you still see errors like the one below or similar,

Build errors in designer.cs file

Build errors in designer.cs file

then follow these steps:

Step 1: Open your project
Web Application versus Web Site

Web Application versus Web Site

Your project must be a Web Application project. If you have a Web Site project, you do no need to worry about designer.cs files, because they won’t be there: a Web Site does not need any, it’s the cleaner and more advanced version of a Web Application.

If you are in doubt, check the picture on the right.

Delete designer.cs file in Visual Studio

Delete designer.cs file in Visual Studio

Step 2: Delete the designer.cs file

When you delete the designer.cs file, the file must be listed like in the picture by Step 1. Right-click the file in the Solution Explorer and select the Delete option. Do not delete the file from disk using the normal Windows Explorer, or Command prompt or whatever. If you did so, you must still right-click the item (now with a warning symbol) and select Delete.

Deleting the file is a harmless action, the file does not contain any valuable information and you should’ve never edited it yourself, because it would be overridden by Visual Studio when auto-generating it again. All it contains are the declarations of the protected fields, which are declared in the ASPX file declaratively.

Step 3: Convert to Web Application
Convert to Web Application

Convert to Web Application

Only if you removed the designer.cs file as described in Step 2, you will find this option when you right-click the project or the ASPX file. After you click this menu option, the designer.cs file will be regenerated for you.

Perhaps a better word for this menu option could’ve been: repair designer.cs. In which case they should’ve made it available at any time, to force-regenerate the designer.cs. Perhaps in Visual Studio 2010?

Remarks

If nothing happens, you may have tried to delete the *.aspx.designer.cs file from Windows Explorer or a Command Window. Don’t do that: you must remove it from inside the Visual Studio environment or this trick will not work at all. If you deleted the file externally, you can still follow the three steps above, as Visual Studio will still show the referenced designer.cs file as a missing file in your Solution Explorer.

Compatibility

This tip works and is tested with the following versions of Visual Studio:

  • Visual Studio 2005;
  • Visual Studio 2008;
  • Visual Studio 2008 Service Pack 1.
  • For migration from .NET 1.1, see the reference of Jan Schreuder below

Credit

Credit for this tip go to Matthias Suter for briefly pointing to this possibility on his blog.

References

Other  references explaining the same idea or extending it are:

– Abel –

  • Pingback: United Kingdomresort bargainseleventh hour lodge hotel offers

  • Pingback: [RESOLVED]downloaded tme picker control ascx | ASP Questions & Answers

  • Pingback: how to hack into someones facebook

  • Pingback: how to hack

  • TheSwears.com

    Awesome, awesome. Thanks very much!

  • thohan

    Well, you saved my bacon, thanks!

  • Eugene

    Thanks!

  • James

    Doesn’t work for me. When converting to web application I get an error in VS: “Exception of type ‘System.Exception’ was thrown.”

  • maurizio

    YOU ARE FANTASTIC !!!!!!

  • Celiker BAHCECI

    Thank you :)

  • Subinal

    Great Tip :) Had been struggling for a while

  • Lizzy

    Thank you, thank you, thank you!! This works like majic. It saved me a lot of time!

  • brale

    perfect gracias

  • http://swsharinginfo.blogspot.in/2012/03/regenerate-designer-file-in-visual.html Sagar

    Hello,
    Its a very nice solution in case of Web Application project.
    @MARK AINSWORTH: In case of website, you need to follow 6 small steps as mentioned in below post.
    http://swsharinginfo.blogspot.in/2012/03/regenerate-designer-file-in-visual.html

    • http://undermyhat.org Abel Braaksma

      Thanks for the tip, it’s nice to have a reference to how to do the reverse (from Web Site instead of from a Web App).

      Interesting to hear that it can happen with Web Site projects, as I stated in this post, I thought it wouldn’t ;) .

  • Ankita

    Thanks Buddy…You saved my life….

  • Igor

    Obrigado, funcionou perfeitamente! =)

  • Sander

    2 years after you posted this article I’m going to ask a question about it because nobody seams to know the answer (I even had contact with some microsoft guys):

    Do you know a way of doing this with winform applications?

  • http://thinq.pl Przemo

    Yes, this is a great article as long as you have one file to regenerate. I have a project with over 300 files to regenerate and I must say that this is a kind of horror. Really, no easy way of doing it?

    • Sander

      Write a little program which deletes all *.designer.cs files?

  • Leslie

    Thank you–that got me closer!

  • Bart Donders

    Regeneration using “Convert to Web Application” failed with a system exception for an aspx-page which was derived from a custom base class (so not directly from System.Web.UI.Page). When I temporarily changed my partial class to derive from System.Web.UI.Page (also made the class in the cs-file empty), regeneration succeeded. Still, later edits in the aspx-page did not show up in the designer file. The connection is effectively lost…

    Bart Donders

    • http://www.undermyhat.org Abel Braaksma

      Bart, what was the system exception you received? Have you tried again with a backup copy and did it yield the same error or did happen only once? What version of Visual Studio and .NET are you using?

      – Abel –

  • coolgal

    Thanks so much!!! Its wonderful…

  • sd

    Thanks a lot it worked for me

  • http://dafran.net Dave Stuart

    Thanks for this! I struggled for 2 hours on this and now I “get it”.

    Thanks
    Dave

  • Milind

    Hi,

    It worked for me. Thanks for the solution.

    Regards,

    Milind

  • Mark Ainsworth

    I tried this. I deleted my aspx.designer.cs file (it showed up as a separate file rather than a ‘subfile’ of the .aspx file). When I right click on the .apsx file, there is no option to convert to a web application.

    • http://www.undermyhat.org Abel Braaksma

      Two things that come to mind:

      1. you are not inside a web application, but inside a web site
      2. because the file appeared as a “subfile”, it was not recognized as a designer file; try showing all files (button on top of solution expl) to see if they’re there already.

      EDIT: I added screenshots for every step to make the process more understandable, see the Step by Step section.

      – Abel –

      • sunil

        Thanks,

Get Adobe Flash player