Here's a summary and an approach that works consistently for me. I've been whacking through this problem for the last day. Given the variation of explanations on this thread, and my own inability to consistently duplicate the problem, I strongly suspect quirks in control designers and the compilation model for web projects contribute to the problem, or at least complicate finding solutions.
Before explanations, my recommendations:
1. If you spend more than 5 minutes on a quirky problem like this in ASP.NET web projects, manually clean the build. You may need to turn off your web server and dump the files in your temporary ASP.NET folder for the site. E.g. Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\My site
2. To get controls to work nicely and easily in web application projects and web site projects, use them the expected "VS 2005 way". You only need to do this if you are having a problem - you don't need to update all 1000 pages in your monster site. The following apply both to the controls and the pages you put them on.
- Use <%@page CodeBehind="x" %>, not <%@page CodeFile="x" %> (see previous post by Maduka ).
- Make sure your controls and pages has a designer (webform.aspx.designer.cs) file. This ensures correct declaration, and is respected by run-time compilation in a way that declarations of controls in your own page are not. Web site projects do not appear to need the designer file.
- Make sure all your pages and controls are declared as "partial" classes, even if you don't have the aspx.designer.cs class present.
3. MS page and control designers sometimes get confused and don't reflect new changes. There are a variety of ways to de-confuse and force them to reset. E.g. go fiddle with something in the design view (e.g. add a control) and make sure that the designer file was updatded.
That said, I haven't been able to consistently get the problem to occur with by going against any of the above recommendations ("CodeFile" in the directive most commonly causes it, as does designer confusion). Using the above recommendations, plus manually cleaning builds, I'm consistently solving it when it rears it's ugly head.
Expanations - my context:
Context: Large VS 2003 web application project ported to VS 2005 "web project". I've been adding user controls (from VS 2005) and geting a problem similar to the thread topic: "The base class includes the field 'WebUserControl1', but its type (common_WebUserControl) is not compatible with the type of control (ASP.common_webusercontrol_ascx)"
Explanations -getting clean builds in web projects
I finally started making progress when I discovered that in a web application project, "clean" and "rebuild" don't mean what I hoped they meant. Here's what I saw:
1) I created two user controls "A" and "B" in a web project.
2) I add a mis-configured control A to a test page. Build and test. Get error message.
3) I change the test page, removing control A entirely and adding control B
4) Execute VS 2005 "Clean" command, VS 2005 "Rebuild" command
5) I'd see the same error, still referring to control A - which should no longer exist on the page.
Conclusion: I'm having problems because the binary associated with the control or page (each gets its own binary) is not up to date. Obviously, this makes it difficult to validate to correctly identify the problem. I cycle through lots of options, only to discover that the previous fix attempt was never applied.
Note that I have not seen this build problem in "web site" projects, only "web projects" with the csproj files. To get a proper rebuild, STOP the web server (also VS 2005 web dev server), then rebuild. I sometimes need to delete the Temporary ASP.NET files for the site as well. To validate that you are I'm working with the rebuilt page, I make some visually apparent change in the code behind (e.g. change control text) and make sure it shows up.
Other posts have suggested they've solved this problem by changing how the web application is published, or deleting the web application entirely: these are other ways of forcing a cleanup of the temporary ASP.NET files.