I’ve ran in to many scenarios where I need to regenerate the code behind for an ASPX or ASCX file. For instance, if you copy the markup from one ASPX page to another ASPX page Visual Studio won’t automatically generate the code behind for any controls that you might have in the markup block you copied. You can force Visual Studio to regenerate code behind but the methods I’ve found are a little kludgy. The one I list here is the only one I’ve tried, perhaps I’ll update this if/when I run into this situation again.

Empty *.designer.cs

The first method I found was to open your *.designer.cs file in notepad and delete the contents. Then go back into Visual Studio and edit your associated markup file. Add some control (i.e. a button) via the markup, then switch to “Design” view and delete the control you just added. This will force Visual Studio to regenerate the *.designer.cs file.

Note that you should probably save the contents of your *.designer.cs file somewhere before you save in notepad!

Advertisements