Like some other SO postings, I am bugged with the error:
The name 'xxx' does not exist in the current context
and I tried lots of things, such as reboot, and clean and rebuild the solution.
The problem started when I moved ("cut" and "paste") a control in the Designer, so that in between, the control did not exist while it still was referenced in the Code *.cs file. Possibly it was invisibly given a slightly different internal windows name.
*.aspx:
<span id="span_msg" runat="server">Msg</span>
*.aspx.cs:
span_msg.InnerText = "the error is...";
*.aspx.designer.cs: this file does not exist, because this is a web site project, not a web application project. Yes, I know, its outdated, but it is too big to change.
Note that this code worked before I moved the control in the Designer. All other controls still work. But also a new control gives the same error.
I tried to analyze this systematically.
First of all, the build gives NO errors. Strange fact is that when debugging with F5, it says: build failed, do you want to continue with the last successful build? (default No)
, and the output window shows: error CS0103: The name 'span_msg' does not exist in the current context.
In the *.cs file, there is no error indication on the name 'span_msg'. Intellisense recognizes this name.
When, in the *.cs file, I rename to 'span_msgXXX', error indications are shown. When I remove the XXX, the error indications dissappear.
From this I conclude that VisualStudio "knows" from the connection between 'span_msg' in the *.cs and in the Designer.
Although this control worked before moving it with the Cut and Paste action, I tried to remove the control entirely, in Designer and in Code, and add a "regular" ASP control from the Toolbox, with a new ID name. Same strange problem with build OK, and the same F5 debugging "build error".
From this I conclude that the problem is not in the control, but in the Page. Somehow, the entire ASP Page is in some erroneous state, where the last, moved, control, and any newly added control, has something wrong.
So what happens differently from the build with no errors to the debugging with F5 and the "build error"? And how to fix this?
I know that the ID that is in the Designer is not the actual ID, and that the system adds some asp postfix to the ID, which is visible with the F12 browser dev tools.
Could it be that my page has started with assigning a new, different, postfix to the control ID's, which confuses the build process?
Update:
I guess, like Greg commented, that VisualStudio just somehow screwed up. As I could dispose of this page, and got my work fixed using another checkout of this page, I will not work further in trying to fix the problem on this particular page.
I did notice that the same error often is shown in the Error List window, with dozens, when an unrelated error occurs due to e.g. a missing semicolon, in an unrelated page or project of the same solution. Very strange indeed.