Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Web Application Project generated CodeBehind .designer file not being updated


:P
On this page:

Raise your hand if this has happened to you (i know it has to me a LOT of times recently): You’re working on a Web Application Project (WAP) page in markup and you paste in some code from another page. All of a sudden you notice that the page’s generated control definitions (in the .designer.cs file) no longer update. You create new controls in markup or the designer and – they don’t show up in your code.

Even though you know the control is defined on the page you’re in red squiggly hell:

MissingControlDefinitions

So now comes the dance: You’re trying to open the markup editor, make some changes. Compile, check again. Open the designer, move something, compile try again… Yup, been there done that – a lot.

What’s the @#%!* Problem?

The problem here is that the designer file doesn’t get re-generated if you have some sort of layout error in the page. Specifically WAP pages look at the page markup and then build the .designer.cs (.vb) file. If there’s a problem in your codebehind with controls not showing up as above the problem usually is that the designer file is empty (if the page was created new with an error) or not updated again.

The most common scenario for me is when I paste code in from another page. For example I have a nearly standard header that I use on some pages that contain common toolbar buttons. They’re not generic enough to put into a control of some sort or on a master page but generic enough that they get pasted around different pages and then customized for the specific page.

The big problem for me is that when I paste between pages ASP.NET is trying to be helpful and add IDs for every server control it pastes into the new page even if that control originally did not contain an id. Even worse apparently ASP.NET generates new ids for you and then fails to check the document if those ids already exist on the page.

For example:

    <div class="toolbarcontainer">
        <a href="../" class="hoverbutton"><asp:Image ID="Image1" runat="server" ImageUrl="~/images/Home.gif"  AlternateText="Home" /> Home</a> | 
        <asp:LinkButton runat="server" ID="lnkRefresh" class="hoverbutton">
            <asp:Image ID="Image1" runat="server" ImageUrl="~/images/Refresh.gif"  AlternateText="Reset Page"/> Reset Page
        </asp:LinkButton> |         
        <small>Page created:&nbsp;<%= DateTime.Now.ToString() %></small><br />
    </div>


this is a simple toolbar header and it contains an image control. In the original page there was no id on the Image control. When pasted however it generates the Image1 id. Only it so happens there’s already another control Image1 on the page (why that is is another point of debate for another day <g>). The short of it is that this is enough for the WAP codebehind engine to fail.

The problem here is that this is difficult to figure out. Nothing tells you WHAT the problem is. You see the error – the compiler is missing the controls – but you get no hint as to why the codebehind page is not behing generated.

Fixing the Problem

When I run into this problem I usually start by deleting the .designer.cs page and hoping that then opening the page in the visual designer and saving will fix any nascent  problems. Sometimes that works – most times it won’t, but it’s worth a try. You may in some cases also get an error dialog.

Next, if the above fails, right click on the ASPX page in the Solution Explorer and select Convert To Web Application. One of two things will happen here (at least for me). You get a most helpful ‘General Exception Error’ in which case you’re screwed for the moment. Or hopefully you get a more useful error message like this one:

ImageExistsDialog

which gives you an idea what the problem is. It’d be really nice if the designer/markup editor could throw this or a similar dialog up for you when it can’t create the .designer file, natch – it requires the explicit Convert To Web Application option after you’ve nuked the designer file.

BTW, if you get the General Exception Failure dialog you may not be completely screwed either. Close the page in the markup editor or designer, recompile (errors and all) and then try again to Convert to Web Application. In my testing most of the time the more helpful error message will come up eventually.

All of this also applies to full site WAP conversions. If pages have errors in them that causes the markup to not compile properly mostly due to naming conflicts or full markup failures you can use this same process to fix pages – one at a time if necessary. I’ve never converted a site to WAP that just worked – usually there are a few oddball pages that require manual ‘intervention’.

This is a nasty problem but also one that is to a degree self-inflicted. If you just use the designer to create new pages it’s unlikely that the WAP codebehind generation will get munged up. As to the duplicate ID problem that is usually the cause of problems for me when cutting and pasting, that is also controllable via configuration option in the Visual Studio options.

 AutoIdOption

This option when checked adds ids to any server controls that get pasted without them. Supposedly this should also fix up ids and check there are no duplicates and most of the time it does, but as in this case apparently it fails to check existing controls on the page when generating new ids. The default for this setting is on.

Posted in ASP.NET  Visual Studio  

The Voices of Reason


 

Valamas
April 07, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

I have a simlar problem in the following cases.
1. You have a button with an empty onclick attribute. I know, duh, but the error list did not show it and the control i was pasting or typing in new onto the form was the focus of the error because Visual Studio refused to create a designer code file reference. Fixed problem by removing or setting onclick property.

2. You have a web user control which has an error in the html markup like the one above. The user control tag becomes the focus of the error after pasting it onto the parent page, rather then the real problem, because yet again, the designer reference has not been created. This one is also the main reason when you try to type the tag for the user control and the tagprefix doesn't list in the intellisence.

This seems to be a problem with Visual Studio and my knowledge is not advanced enough to pick it.

Until i learned these lessons, i was ripping my hair out at the delay of writing the application.

Zubair.NET!
April 07, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

This has happened to me alot but changing a few IDs here or there mostly fixes the problem so have never given it much thought.

The Luddite Developer
April 08, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

Microsoft are driving their development tools forward at a hectic pace.

Every day we read about new releases like ASP.NET MVC 1.0, new beta's for Silverlight 3, Visual Studio 2010, Windows 7 etc.

Whilst it is nice to know that our development tools will have a future and that there will be plenty of new features to play with, I think that in some instances we would very much appreciate more regular bug fixes to our current production tools.

A monthly bug fix update for the current RTM release of VS would not go amiss, as long as the install worked fairly seemlessly and the appropriate checks were made for possible incompatibilities before the update was started.

Adam Tibi
April 08, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

Been there and done that. Such a waste of time! Thank you for documenting the behaviour.

mk
April 08, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

Another possible cause: The IDs are case insensitive. When i have a class member (C#) that just differs in its casing from an existing ID, the .designer.cs file is also not regenerated. Took me quite some time (and nerves, because first new controls did not appear and then i did not know about the "convert to web application" trick) to figure this out.


(mk)

Ian Patrick Hughes
April 09, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

This has been DRIVING ME CRAZY.

I find myself having to add controls in the designer file manually, which is not that big of deal, but irritating. Swapping to the design view and back to source sometimes does it, sometimes not. Making the page dirty, saving, and then doing what I want to do sometimes fixes it. Sometimes not.

I think its that level of inconsistency that drives me bonkers. It has to have something to do with how Visual Studio builds its memory cache, maybe? If you close the VS instance and re-start it the problem seems to be gone as well.

Roshan
May 21, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

There is no "Convert to Web Application" option when I right-click my aspx file in the solution explorer. I only see that option if I right-click on my project file. Would this have the same effect?

Roshan
May 21, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

I tried the above and it doesn't seem to work at all. Any other ways I could fix this?

Vera
July 15, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

There is no such thing as "Convert to Web Application". Also, have you noticed that it seems like the problem starts relaltively "small" - the controls on the design form are not recognized in the code-behind page and .aspx.designer file is being visible in the Solution Explorer for just one of the pages. The problem than seem to propogate: First you can still complile the app, then you no longer can. With each complie attempt the rest of the pages seem to get the same issue, etc.

As a matter of fact, after following the above instructions I no longer can open the aspx.vb file. So yes, the above solution was not solution at all. It has been somewhat useful to read such a detailed and accurate representation of what I have been experiencing today - very maddening. To add to the documentation of the problem, (unfortinetely I have no solution as well) I have made the following serious of steps prior to the problem appearing, so the root might be somewhere there:

1. Original (Production) web app was created as a Solution (File --> New --> Solution);
2. Created a Development web app by first creating a new Web Site (File --> New -->Web Site) in the same location by different name;
3. Closed the Development web app and Visual Studio 2008;
4. Using Windows Explorer copied all the files from the Production app into the folder of the newly create Development app;
5. Open Development app in Visual Studio - all works fine so far;
6. Did some cosmetic changes;
7. Added Combo Box control from AJAX Control Toolkit;
8. Changed code in code-behind pages (two pages) to accomodate new functionality;
9. Compiled the project (no errors) and uploaded on the test Development server (FTP) and tested - All works fine!
10. Two days later opened up the app to make some cosmetic changes. Changed some spelling (HTML), uploaded on the server, tested, had my boss look at it and a few co-workers - all works great!
11. Opened the project again and this time noticed the .desinger files that never saw before, opened one of the code-behind classes and saw the squigly blue lines - also this was not there before
12. All when downhill from there.

Aaron
October 14, 2009

# re: Web Application Project generated CodeBehind .designer file not being updated

Actually there is a Convert to Web Application, you just have to delete designer class first.

Thanks though, this was helpful. You want to look out if you register any user controls on the page you're pasting from, and remember to insert the <%@ Register directive into the page you're pasting onto.

Brad
January 18, 2010

# re: Web Application Project generated CodeBehind .designer file not being updated

Same issue - Different fix. In my case I am creating an .ascx control and have been running into the same *.designer.cs wtfisgoingonhere code generation issues. I have found that if you rename the .ascx (assumed to work with .aspx) that the .designer.cs file will regenerated with all the proper controls. Then I just rename it back again to what it was before. Seems to work (disclaimer: if you have xml syntax errors in your .ascx / .aspx files it probably wont work).

Chris
February 16, 2010

# re: Web Application Project generated CodeBehind .designer file not being updated

I am going totally insane. I'm using Visual Studio 2010 RC and the f#f*ng code behind designer will not generate controls that are embedded in tab pages using TabContainer, despite me trying all the tricks that appear in this thread. This is a complete show stopper and forces me back to VS 2008.

Tod Nordell
March 24, 2010

# re: Web Application Project generated CodeBehind .designer file not being updated

Dear Rick,

Sounds similiar, or equal, to a strange phenomenon i discovered last week when i opened a .sln with a bunch of different layers and a gui, which in fact is running in production without fuzz.
When the shit was fully loaded i created a new content page and dragged some controls to it and to my surprise none of the seemed to be recognized by the studio 2008 even though they were asp:XXX. I switched to another old page and found the same phenomenon. I've re-referenced the System.Web namespace and have tripple checked the main .config but no luck there. I will try your suggestion today so hopefully it'll work out as planned. I'll be back Bennet!

Yours,
Tord

John Dang
April 07, 2010

# re: Web Application Project generated CodeBehind .designer file not being updated

Microsoft should get rid off the CodeBehind .designer and let developer to manupulate this file just like any other files. I have to delele a whoe page and type ping again and again and hope it will generate correct codes in CodeBehind .designer file.

Paul Jones
May 03, 2010

# re: Web Application Project generated CodeBehind .designer file not being updated

I was pulling my hair out for hours on this on a CreateUserWizard page. Solution for me was to cut the fields from the designer file into my codebehind class and delete the designer file.

For example, add an asp:textbox fName to the markup and then declare it in the codebehind:
Protected WithEvents fName As Global.System.Web.UI.WebControls.TextBox - No more lost controls!

I'd only use this with pages that are particulary mean.

Erosha Meegahapola
May 18, 2010

# re: Web Application Project generated CodeBehind .designer file not being updated

I had the same problem. But I just forced my code behind to use the control. Then build the project and there were no errors on compil. Then I closed and opened th project. Simple as that.

Website Development
May 23, 2010

# Website Development

Therefore you have worked hard in making your web site, often adding substance to it, and building links and increasing search engine placements. The following step, that many fail to notice, is to capture your guests by utilizing an automatic responder. If you would like to avoid all the problems associated with setting up your websites online then read my LPGen2 review for more information.

Mark
February 04, 2011

# re: Web Application Project generated CodeBehind .designer file not being updated

I find that right clicking on one of the controls that are marked as in error in the designer and choosing 'Refresh' from the menu will cause the designer.cs file to be re-generated.

lni
August 18, 2011

# re: Web Application Project generated CodeBehind .designer file not being updated

I have found that if I open the Designer file via right clicking in the code and choosing "View Designer" or right click the file in Solution Explorer and choose "View Markup", it will open in a mode where it will behave correctly.
I have to open the file this way everytime...

Dot Net Developer
October 24, 2012

# re: Web Application Project generated CodeBehind .designer file not being updated

Gotta till I've been there and it drove me nuts! I probably spent my whole evening trying to figure out the problem.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024