How do you force Visual Studio to regenerate the .designer files for aspx/ascx files?

400

Sometimes when I'm editing page or control the .designer files stop being updated with the new controls I'm putting on the page. I'm not sure what's causing this to happen, but I'm wondering if there's any way of forcing Visual Studio to regenerate the .designer file. I'm using Visual Studio 2008

EDIT: Sorry I should have noted I've already tried:

  • Closing & re-opening all the files & Visual Studio
  • Making a change to a runat="server" control on the page
  • Deleting & re-adding the page directive

This question is tagged with asp.net visual-studio visual-studio-2008

~ Asked on 2008-09-05 06:21:06

46 Answers


372

If you open the .aspx file and switch between design view and html view and back it will prompt VS to check the controls and add any that are missing to the designer file.

In VS2013-15 there is a Convert to Web Application command under the Project menu. Prior to VS2013 this option was available in the right-click context menu for as(c/p)x files. When this is done you should see that you now have a *.Designer.cs file available and your controls within the Design HTML will be available for your control.

PS: This should not be done in debug mode, as not everything is "recompiled" when debugging.

Some people have also reported success by (making a backup copy of your .designer.cs file and then) deleting the .designer.cs file. Re-create an empty file with the same name.

There are many comments to this answer that add tips on how best to re-create the designer.cs file.

~ Answered on 2008-09-05 06:25:29


96

Well I found a solution that works, though I don't really like it. I had to delete the .designer.cs file then recreate an empty file with the same name. When I went back in and saved the aspx file again, the designer file was re-generated.

Dodgy!

~ Answered on 2008-09-05 06:32:13


79

I use the following method which works everytime:

  • Select all of the code-in-front (html markup etc) in the editor of the aspx/ascx file.
  • Cut.
  • Save.
  • Paste.
  • Save.

Recompile.

~ Answered on 2009-01-29 16:19:44


65

I recently saw that I was having the same problem. Visual Studio 2010 was refusing to update the designer file.

As it turns out, VS doesn't modify the designer file for a page that uses CodeFile (run off of pages) instead of CodeBehind (DLL). This is true no matter how many times you close VS, reload the project, re-create the control(s), or modify a file. Nothing would prompt VS to regenerate the designer. It's as if it doesn't create the designer file for CodeFile pages but does require it to be there.

I changed it to CodeBehind and saved the page. The designer file updated immediately. Then I just changed it back and everything was still golden. This behavior seems to be new with VS 2010 / .NET 4.0 as VS 2008 by default didn't suffer from this.

It's this part:

<%@ Page Language="vb" AutoEventWireup="false" CodeFile="YourPage.aspx.vb" Inherits="YourPageClass" %>

Change CodeFile to CodeBehind, save, and then revert.

~ Answered on 2011-01-22 20:18:08


38

There is another possibility: You may have an error in your .aspx file that does not allow Visual Studio to regenerate the designer.

If you switch to Design View, it will show the control as unable to be rendered. Fixing the control (in my case it was an extra quote in the properties) and recompiling should regenerate the designer.

~ Answered on 2008-10-06 22:05:54


22

Most of the solutions here don't work if you're running Visual Studio 2013 and possibly 2012. Microsoft probably introduced some optimizations to make the IDE snappier, consequently they've reduced the number of cases that trigger the code generator. The following scenarios that used to work no longer do:

  1. Delete the aspx or ascx file -- No longer checks for this case
  2. Cut all the content and repaste into the aspx or ascx file -- No longer works, no change in the references
  3. Convert to Web Application -- Option no longer available
  4. Arbitrarily changing content on the aspx/ascx file -- No longer works (see 2).

The solution is surprisingly simple, but it's slightly cumbersome. In order to trigger the code generator, change something that would require the designer.aspx.cs to be generated. Changing content that doesn't affect code, such as a CSS style or adding text, won't trigger the code generator. You must change a referenced control. Here's how to do it:

In the ascx or aspx change the ID of the control

<asp:HyperLink ID="MyLink" runat="server" NavigateUrl="~/Default.aspx" Text="Home" />

to

<asp:HyperLink ID="theLINK" runat="server" NavigateUrl="~/Default.aspx" CssClass="tab" Text="Home" />

Go to the ascx.cs or aspx.cs and make sure you rename all references to "MyLink" to "theLINK" as well. Save and do build and the you should be good to go.

~ Answered on 2014-01-29 23:36:02


14

the only way I know is to delete the designer file, and do a convert to web app. However when you do this, it usually pops up with the error, as to why it didn't auto-regen in the first place, its usually a control ref that isn't declared in the head of the page.

~ Answered on 2008-09-09 14:28:10


9

Convert to Web Application did not work for me.

Deleting designer.cs and pasting a blank designer.cs did not work either.

But yes this worked:

  1. Select all(Default.aspx)
  2. Cut
  3. Save Default.aspx
  4. Paste
  5. Save Default.aspx

Done. New designer.cs generated. :)

~ Answered on 2014-02-14 09:23:04


6

I often found that copy/pasting caused this behaviour for me. Most cases can be solved by editing the ID of a server control (just add a character, then delete it).

Also remember that control inside things like Repeaters aren't visible in the designer file.

And yes, there are cases where you need to do the delete-the-file magic listed above - but the name-change solution will work most of the time.

~ Answered on 2009-05-11 02:57:54


5

My experience is that if you want to do like in this article, like stated above.

Your markup file (aspx/ascx) has to include the CodeBehind="MyPage.aspx.cs" attribute or else it won´t work. I blogged about it here.

~ Answered on 2009-03-26 20:08:42


5

I've found a way to solve this problem without changing any code or running commands like "Convert to Web Application" - and it's simple too!

What I found was that restarting Visual Studio often solves the problem, but sometimes it doesn't. In those cases, if you close Visual Studio and then delete all content in the "obj" directory for the web project before you open it again, it has always worked for me.

(when started again you just add a space and remove it again and then hit save to have the designer file properly regenerated)

~ Answered on 2013-02-01 09:21:42


4

When you are in design view, right click on the screen and hit refresh.

~ Answered on 2010-09-14 06:08:43


4

Another thing which worked was -

  1. Manually delete & then Create a designer file in filesystem.
  2. Edit Project file.
  3. Add code to include designer
    Eg: <Compile Include="<Path>\FileName.ascx.designer.cs"> <DependentUpon>FileName.ascx</DependentUpon> </Compile>
  4. Reload Project
  5. Open as(c/p)x file in design/view mode & save it.
  6. Check designer file. Code will be there.

~ Answered on 2013-11-12 05:17:36


4

(The following comes from experience with VS2005.)

If you edit an ASPX page while debugging, then the codebehind doesn't get updated with the new classes. So, you have to stop debugging, trivially edit the ASPX page (like add a button or something), then click Design View, then delete the button. Then, your designer files should be updated.

If you are having a different issue with VS2008, then I can't help.

~ Answered on 2008-09-05 06:26:24


3

If you are using VS2013 or later , make sure that the code referenced with attribute "CodeBehind" not "CodeFile", then do below steps

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="yourControl.ascx.cs" Inherits="yourControl.yourControl" %>
  • create empty designer page (or clear it if it's already exists "yourControl.ascx.designer.cs")
  • in the ascx (or aspx) copy all code , the delete it, then save. re-past it again , then save.
  • the designer file should be populated now.

~ Answered on 2015-02-11 18:56:30


2

  1. Step 1: Select all your aspx code, Cut [ CTRL+X ] that code and Save.

  2. Step 2: Again paste the same code in the same page and save again

Now your .desinger page will refresh with all controls in .aspx page.

~ Answered on 2019-07-16 09:24:40


2

Within the Visual Studio:

1) Remove your aspx.designer.cs file

2) Right click on your aspx file and select "Convert to Web Application" This should add the aspx.designer.cs back and up to date.

If you get an error saying:

"Generation of designer file failed: The method or operation is not implemented."

Try close Visual Studio and then reopen your project and do step number two again

How to generate aspx.designer.cs in visual studio?

~ Answered on 2015-04-17 06:43:55


2

Just to add to the long list of answers here - I've just run into this issue in VS2010 (SP1) with an .aspx file. I tried adding and removing standard ASP controls (which has worked in the past) but in the end, I had to remove one of the runat=server lines from an existing control (and save) to force the designer file to regenerate.

~ Answered on 2012-06-28 13:44:13


2

I've encountered the same problem for years now, working in Visual Studio 2008. And I've tried every "solution" on StackOverflow and dozens of blogs, just like I'm sure all of you have. And sometimes they work, and sometimes they don't, just like I'm sure all of you have encountered. And apparently it's still an issue in VS2010 and VS2012.

So finally, a couple of months ago, I decided enough was enough, and over a few weeks I built a tool called "Redesigner" that generates .designer files. It's open-source under the BSD license, with the source code available on SourceForge — free to use, free to steal, free to do anything you please with. And it does what Visual Studio fails to do so often, which is generate .designer files quickly and reliably.

It's a stand-alone command-line tool that parses .aspx and .ascx files, performs all the necessary reflection magic, and spits out correct .designer files. It does all the parsing and reflection itself to avoid relying on existing code, which we all know too well is broken. It's written in C# against .NET 3.5, but it makes pains to avoid using even System.Web for anything other than type declarations, and it doesn't use or rely on Visual Studio at all.

Redesigner can generate new .designer files; and it offers a --verbose option so that when things go wrong, you get far better error messages than "Exception of type System.Exception was thrown." And there's a --verify option that can be used to tell you when your existing .designer files are broken — missing controls, bad property declarations, unreadable by Visual Studio, or otherwise just plain borked.

We've been using it at my workplace to get us out of jams for the better part of the last month now, and while Redesigner is still a beta, it's getting far enough along that it's worth sharing its existence with the public. I soon intend to create a Visual Studio plugin for it so you can simply right-click to verify or regenerate designer files the way you always wished you could. But in the interim, the command-line usage is pretty easy and will save you a lot of headaches.

Anyway, go download a copy of Redesigner now and stop pulling out your hair. You won't always need it, but when you do, you'll be glad you have it!

https://sourceforge.net/projects/redesigner/

~ Answered on 2013-01-21 06:23:37


2

in solution explorer just right click and select convert to web application. It will generate all the designer files again.

~ Answered on 2016-03-17 20:48:28


2

TL;DR;
Edit the Inherits attribute of the ASPX page's @Page directive and hit Save. Your designer file should be regenerated.

Ensure that Inherits = <namespace>.<class_name> and CodeBehind = <class_name>.aspx.cs


I was trying to do this on a Sharepoint 2010 project, using VS 2010 and TFS, and none of the solutions above worked for me. Primarily, the option, "Convert to Web Application" is missing from the right-click menu of the .ASPX file when using TFS in VS 2010.

This answer helped finally. My class looked like this:

namespace MyProjects.Finance.Pages
{
    public partial class FinanceSubmission : WebPartPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        // more code
    }
}

And my @Page directive was (line-breaks here for clarity):

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="FinanceSubmission.aspx.cs"
    Inherits="MyProjects.Finance.Pages.FinanceSubmission"
    MasterPageFile="~masterurl/default.master" %>

I first changed the Inherits to MyProjects.Finance.Pages, hit Save, then changed it back to MyProjects.Finance.Pages.FinanceSubmission and hit Save again. And wallah! The designer page was regenerated!

Hope this helps someone using TFS!

~ Answered on 2014-12-19 14:58:47


2

Here is wat i experienced , Select the website folder right click in the Solution Explorer, select Convert to Web application for all the aspx file a designer file will get generated.

Sameer

~ Answered on 2010-01-15 19:30:20


2

  • Select-all in the designer file and delete everything in the file, leaving it blank and then save
  • Select-all in the ASPX/ASCX file and cut everything and then re-paste it back
  • The designer file should have regenerated the code

~ Answered on 2008-10-06 22:09:16


1

  1. Close designer.cs.

  2. Change your aspx file to design view.

  3. Right-Click –> Refresh.

  4. Save

~ Answered on 2017-10-19 12:52:54


1

One thing that nobody's mentioned is to visit the page. I had my designer file stop regenerating because I was including a user control that didn't exist (for various reasons), and none of the things suggested here worked. For whatever reason, I didn't get any errors in Visual Studio - besides the one complaining about the fact that my client-side controls didn't exist because they weren't being regenerated, of course.

It took actually visiting the page to get ASP.Net to tell me the error.

~ Answered on 2012-05-17 23:11:29


1

The solution the worked for me is:

I just copied the page and and pasted it in the same portion, then renamed the first page(what ever name) and renamed the copied page as the original page. Now the controls are accessible.

~ Answered on 2012-05-21 10:18:02


1

I had the problem that my new controls would not generate in the designer file when declared in the .ascx file. The problem was that i declared them in the code behind also. So deleting the declaration in the code behind solved my problem.

~ Answered on 2011-07-19 10:51:21


1

I had two problems... outdated AJAXControlkit - deleted the old dll, removed old controls from toolbox, downloaded new version, loaded toolbox, and dragged and dropped new controls on the page (see http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_24591597.html)

Also had misspelling in my label control (had used 'class' instead of 'cssclass').

Ta

~ Answered on 2010-07-08 04:55:37


1

This can also happen if you update the namespace and don't update the namespace in the designer file. Fix: Update the namespace in the designer file too.

~ Answered on 2012-07-13 08:51:40


1

If you are like me and you add old .ASPX files to a more recent project. You are probably going to forget some of the controls used on the page.

If so, first thing, if there are multiple files you are installing; Fix one at a time first.

When you compile, fix errors generated. They will probably be the same errors in all the files.

Next, if you have Designer files, delete all of the inserted - designer files. Next, make sure there are not any other errors when you compile, other than the designer files.

Finally right click your web project and click on Convert to Web Application. This will insert the designer files you need.

These are the absolute best steps to fix the issues.

~ Answered on 2011-06-02 02:46:25


1

Delete the designer.cs file and then right click on the .aspx file and choose "Convert To Web Application". If there is a problem with your control declarations, such as a tag not being well-formed, you will get an error message and you will need to correct the malformed tag before visual studio can successfully re-generate your designer file.

In my case, at this point, I discovered that the problem was that I had declared a button control that that was not inside of a form tag with a runat="server" attribute.

~ Answered on 2009-02-10 19:37:53


1

This is a bug in the IDE; I've seen it since VS 2003. THe solution is simple though.

Save your files. Completely exit the IDE (make sure the process stops, task mgr.)

Reopen the solution, dirty the markup, save. Fixed.

~ Answered on 2010-04-30 06:25:09


0

For VS2015... here's a VB example for switching from a WebSite project to a Web Application Project that worked for me. None of the other solutions worked for me, which is why I'm sharing this. It's not elegant, but works.

Step 0: Replace all " CodeFile=" with " CodeBehind=" in your project Step 1: Close the Solution. Step 2: Run a home-grown Windows app with the following code (below steps). Step 3: Re-open the solution. Step 4: In Solution Explorer, make sure you are Showing All Files, and search for designer.vb (or designer.cs for C#) Step 5: Select all the missing files, and include them in your project. Step 6: For each file, view the main page / control and save it.

    private void button1_Click(object sender, EventArgs e)
    {
        ProcessDirectory(new DirectoryInfo(textBox1.Text));
    }

    private void ProcessDirectory(DirectoryInfo directory)
    {
        ProcessMask(directory, ".ascx", ".vb");
        ProcessMask(directory, ".aspx", ".vb");

        foreach (DirectoryInfo directoryInfo in directory.GetDirectories())
            ProcessDirectory(directoryInfo);
    }

    private void ProcessMask(DirectoryInfo directory, string maskStart, string maskEnd)
    {
        FileStream fs;
        foreach (FileInfo file in directory.GetFiles(string.Format("*{0}{1}", maskStart, maskEnd)))
        {
            string designerFileName = file.Name.Replace(string.Format("{0}{1}", maskStart, maskEnd), string.Format("{0}.designer{1}", maskStart, maskEnd));
            if (directory.GetFiles(designerFileName).Length == 0)
            {
                using (fs = File.Create(Path.Combine(directory.FullName, designerFileName)))
                {
                    fs.Close();
                }
            }
        }
    }

~ Answered on 2017-01-17 15:17:09


0

One of my solution, which works for me.

  1. Close the Visual Studio
  2. Remove hidden .vs directory
  3. Remove existing .sln file
  4. load the project in visual studio using .csproj file

~ Answered on 2018-07-11 06:30:28


0

In Visual Studio 2019 below are the steps:

  1. Create webform1.aspx
  2. Copy your aspx code and paste in webform1.aspx
  3. Compile your project you should have designer code inside webform1.aspx.designer.cs
  4. Now you can copy designer code and use in your original webform.

~ Answered on 2020-08-01 14:45:56


0

I thought I had this problem and was tearing my hair out, but then I realised that I was trying to reference a control from a static method.

Making the method non-static resolved the issue for me.

~ Answered on 2017-04-12 15:21:13


0

I've had this problem a lot, and just did again. I tried fixing it using these suggestions, and nothing worked. I finally found that I had the 'Title' attribute in the page header twice(I added to the end, not realizing that VS added a blank Title="" to the beginning)-- removing the extra attribute caused VS2008 to re-generate the designer file... I hope VS2010 fixes this problem, letting us know why the designer file generation isn't happening...

-- Derek

~ Answered on 2010-09-09 16:36:56


0

In my case I was just missing a register TagPrefix at the top. Somehow the previous dev worked without having this in there?

~ Answered on 2010-06-17 17:42:10


0

I know I'm late to the party, but I thought if after trying the accepted answer by @Glenn Slaven and the current highest rated answer by @Espo you are still out of luck, the following might save some people out there some trouble.

User Controls (.ascx) are what constantly stop auto-generating for me. I've found that in the instances where I use other User Control(s) in my User Control, it breaks the auto-generation.

The solution I came up with was to use the trick we all know of for getting IntelliSense to work in User Controls or Skin files when using CSS classes from external sources. Instead of just referencing the CSS file, now I Register the other User Control(s) that I need. It looks something like this:

<% If False Then %>
<%@ Register TagPrefix="uc" TagName="Title" Src="~/controls/title.ascx" %>
<link rel="stylesheet" type="text/css" href="../css/common.css"  />
<% End If %>

After that, I Save the file, which prompts the auto-generation to regenerate, and I'm back up and running.

~ Answered on 2011-06-01 18:09:04


0

In my case, it was fixed when I added a CodeBehind to the @Page section.

~ Answered on 2011-04-29 16:13:19


0

I know this is an old topic but I just wanted to add a solution that wasn't suggested yet.

I had the same problem with a resource file. I edited it outside Visual Studio and the designer file hadn't updated properly.

Renaming the file did the trick of regenerating the Designer file. I just renamed it to the initial name again and that worked just fine!

~ Answered on 2013-06-24 08:06:48


0

I'm in VS 2003 and none of these worked for me. What worked was to open the code at the top of the .vb page in the section labeled Web Form Designer Generated Code (the part that says not to edit there) and declare it there, where the system declared all the other controls. Bizzare.

~ Answered on 2013-08-14 15:13:24


0

  1. replace your custom tag with a invalid tag name. Save it
  2. restore the invalid tag name back to custom tag name. Save it. Then you will be prompted to checkout the *.designer.cs files(or silently modify the designer.cs) and produce correct variable of custom tag control.

~ Answered on 2010-01-29 07:13:41


0

I have had this issue before. I usually just hit enter to add a line and then wait for the plus/minus to show on the html page and the designer should add what you need. I have also had to close the project and reopen it to get it to work.

~ Answered on 2010-07-06 15:08:15


0

I had this problem and for me, I had a space in one of my ID values for one of my controls. I took the space out and the designer file regenerated itself.

~ Answered on 2010-02-21 22:58:57


0

Apart from all the good answers already given, I'd like to add to @johan-leino's great answer. In my case, for some arbitrary reason, the CodeBehind attribute was omitted from the @Page directive/.aspx file. Likewise, it might be worthwhile to check the CodeFile attribute for @Control directives/.ascx files (obviously in conjunction with an Inherits attribute in both cases).

Depending on the exact scenario and reason required to 'force' a regenerate of .designer.cs, one could also try to format the document (potentially indicating a parsing error) before (quick) saving (regardless whether there were any changes or not) and check the Error List for warnings.

~ Answered on 2010-11-24 06:38:01


Most Viewed Questions: