[c#] 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.

This question is related to c# asp.net visual-studio-2005 code-behind

The answer is


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


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">
    .
    .
    .
</asp:Repeater>

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


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.


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


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:

runat="server"

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.


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.


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


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!


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.


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!


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


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

AutoGenerateColumns="False"DataKeyNames="ProductID"

instead of

AutoGenerateColumns="False" DataKeyNames="ProductID"

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.


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.


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.


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.


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.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to visual-studio-2005

How to generate List<String> from SQL query? Unable to copy file - access to the path is denied How do I print to the debug output window in a Win32 app? How to debug a referenced dll (having pdb) What is the difference between Release and Debug modes in Visual Studio? The name 'controlname' does not exist in the current context How do I programmatically get the GUID of an application in .NET 2.0 Cannot find Dumpbin.exe How to fix "Referenced assembly does not have a strong name" error? ERROR : [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Examples related to code-behind

How to call a C# function from JavaScript? how to access master page control from content page Set Text property of asp:label in Javascript PROPER way ASP.NET Web Application Message Box How to programmatically set the Image source Accessing a resource via codebehind in WPF Adding css class through aspx code behind Passing arguments to JavaScript function from code-behind The name 'controlname' does not exist in the current context ASP.net page without a code behind