The name ‘controlname’ does not exist in the current context

I have a web application that I’m working on (ASP.NET2.0 with C#, using VS2005). Everything was working fine, and all of a sudden I get the error:

Error 1 The name 'Label1' does not exist in the current context

and 43 others of the sort for each time that I used a control in my codebehind of the page.

This is only happening for 1 page. And it’s as if the codebehind isn’t recognizing the controls. Another interesting thing is that the intellisense isn’t picking up any of the controls either..

I have tried to clean the solution file, delete the obj file, exclude the files from the project then re-add them, close VS and restart it, and even restart my computer, but none of these have worked.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I know this is an old question, but I had a similar problem and wanted to post my solution in case it could benefit someone else. I encountered the problem while learning to use:

  • ASP.NET 3.5
  • C#
  • VS2008

I was trying to create an AJAX-enabled page (look into a tutorial about using the ScriptManager object if you aren’t familiar with this). I tried to access the HTML elements in the page via the C# code, and I was getting an error stating the the identifier for the HTML ID value “does not exist in the current context.”

To solve it, I had to do the following:

1. Run at server

To access the HTML element as a variable in the C# code, the following value must be placed in the HTML element tag in the aspx file:


Some objects in the Toolbox in the Visual Studio IDE do not automatically include this value when added to the page.

2. Regenerate the auto-generated C# file:

  • In the Solution Explorer, under the aspx file there should be two files: *.aspx.cs and *.aspx.designer.cs. The designer file is auto-generated.
  • Delete the existing *.aspx.designer.cs file. Make sure you only delete the designer file. Do not delete the other one, because it contains your C# code for the page.
  • Right-click on the parent aspx file. In the pop-up menu, select Convert to Web Application.

Now the element should be accessible in the C# code file.

Method 2

Check your code behind file name and Inherits property on the @Page directive, make sure they both match.

Method 3

exclude any other pages that reference the same code-behind file, for example an older page that you copied and pasted.

Method 4

I had the same problem. It turns out that I had both “MyPage.aspx” and “Copy of MyPage.aspx” in my project.

Method 5

Also, make sure you have no files that accidentally try to inherit or define the same (partial) class as other files. Note that these files can seem unrelated to the files where the error actually appeared!

Method 6

I ran into this same error, except it was a WPF error. I was rearranging projects and had a control defined in like this:

<local:CustomControl Name="Custom" />

In my code behind I tried using Custom.Blah, but I got the error:

The name 'Custom' does not exist in the current context

What did the trick for me was changing my control in Xaml to this:

<local:CustomControl x:Name="Custom" />

Hope this helps someone out there!

Method 7

I get the same error after i made changes with my data context. But i encounter something i am unfamiliar with. I get used to publish my files manually. Normally when i do that there is no App_Code folder appears in publishing folder. Bu i started to use VS 12 publishing which directly publishes with your assistance to the web server. And then i get the error about being precompiled application. Then i delete app_code folder it worked. But then it gave me the Data Context error that you are getting. So i just deleted all the files and run the publish again with no file restrictions (every folder & file will be published) then it worked like a charm.

Method 8

I had the same issue, my problem was not having space between two attributes”


instead of

AutoGenerateColumns="False" DataKeyNames="ProductID"

Method 9

I fixed this in my project by backing up the current files (so I still had my code), deleting the current aspx (and child pages), making a new one, and copying the contents of the backup files into the new files.

Method 10

this error often occurs when you miss runat="server" .

Method 11

Solution option #2 offered above works for windows forms applications and not web aspx application. I got similar error in web application, I resolved this by deleting a file where I had a user control by the same name, this aspx file was actually a backup file and was not referenced anywhere in the process, but still it caused the error because the name of user control registered on the backup file was named exactly same on the aspx file which was referenced in process flow. So I deleted the backup file and built solution, build succeeded.

Hope this helps some one in similar scenario.

Vijaya Laxmi.

Method 12

I had the same issue since i was tring to re produce the aspx file from a visual studio 2010 project so the controls had clientidmode=”Static” property. When this is removed it was resolved.

Method 13

I had a similar problem when tweaking with a Repeater after converting it from a DataList.

Problem was that I accidentally united 2 attributes when deleting an unneeded one.

<asp:Repeater runat="server" ID="ClientsRP"DataSourceID="ClientsDS">

And this prevented the generation of the repeater in the design file.

Method 14

I had the same error message. My code was error-free and working perfectly, then I decided to go back and rename one of my buttons and suddenly it’s giving me a compile error accompanied by that blue squiggly underline saying that the control doesn’t exist in current context…

Turns out Visual Studio was being dumb, as the problem was related to the backup files I had made of my aspx.cs class. I deleted those and the errors went away.

Method 15

I ran into this same issue. Apparently, you shouldn’t call a class in the BLL the same name as one of the .aspx/.aspx.cs files. I thought they would not be in the same scope, etc. but it messed with Visual Studio’s internal workings too much. I’m a bit surprised there isn’t something to keep you from doing this if it is going to produce that type of error. Anyway, just delete the .aspx/.aspx.cs files and rebuild your project. Then bring them back in under another name. You can copy/paste your code into another editor if you don’t want to retype it all back in.

Method 16

In my case, when I created the web form, it was named as WebForm1.aspx and respective names (WebForm1). Letter, I renamed that to something else. I renamed manually at almost all the places, but one place in designer file was still showing it as ‘WebForm1’.

I changed that too and got rid of this error.

Method 17

1) Check the CodeFile property in <%@Page CodeFile=”filename.aspx.cs” %> in “filename.aspx” page , your Code behind file name and this Property name should be same.

2)you may miss runat=”server” in code

Method 18

In my case I had to hunt through the 417 “controlname not found” errors to find an actual error: I had replaced a DLL but not updated the version number in the web.config. Fixed that and built successfully, 3 minutes after that all the other errors had resolved themselves.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x