Web Application Project generated CodeBehind .designer file not being updated
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:
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: <%= 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:
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.
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.
Other Posts you might also like
- Adding minimal OWIN Identity Authentication to an Existing ASP.NET MVC Application
- Resolving Paths To Server Relative Paths in .NET Code
- Map Physical Paths with an HttpContext.MapPath() Extension Method in ASP.NET
- Back to Basics: Rewriting a URL in ASP.NET Core
- Getting the Client IP Address in ASP.NET Core
The Voices of Reason
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
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.
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
(mk)
# re: Web Application Project generated CodeBehind .designer file not being updated
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.
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
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.
# re: Web Application Project generated CodeBehind .designer file not being updated
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.
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
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
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
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.
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
# re: Web Application Project generated CodeBehind .designer file not being updated
I have to open the file this way everytime...
# re: Web Application Project generated CodeBehind .designer file not being updated
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.