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.
<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
<small>Page created: <%= DateTime.Now.ToString() %></small><br />
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