The name 'controlname' does not exist in the current context
Asked Answered
T

18

80

I have a web application that I'm working on (ASP.NET 2.0 with C#, using Visual Studio 2005). 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 code behind page.

This is only happening for one page. And it's as if the code behind page 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 Visual Studio and restart it, and even restart my computer, but none of these have worked.

Trample answered 1/4, 2009 at 17:34 Comment(2)
Also check the namespace in the designer.cs sometimes after renaming a aspx page the namesapce is simply removed from the designer.cs.Barrelchested
This might occur after copying markup for the control from other .aspx file. I deleted the copied markup and manually wrote control markup and error vanished.Diageotropism
A
48

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

Arundinaceous answered 1/4, 2009 at 17:37 Comment(1)
Same here, with a small addition: Make sure you're checking that it has the fully qualified class name. For some reason VS changed mine to Inherits="KioskB" when it needed to be "MyNamespace.KioskB". It looked right until I compared the markup to another one of the pages and noticed the namespace part was gone.Vesicle
S
122

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 or Project menu. In the pop-up menu, select Convert to Web Application.

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

Stingo answered 30/12, 2009 at 17:3 Comment(0)
A
48

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

Arundinaceous answered 1/4, 2009 at 17:37 Comment(1)
Same here, with a small addition: Make sure you're checking that it has the fully qualified class name. For some reason VS changed mine to Inherits="KioskB" when it needed to be "MyNamespace.KioskB". It looked right until I compared the markup to another one of the pages and noticed the namespace part was gone.Vesicle
F
34

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

Falcone answered 13/10, 2009 at 14:15 Comment(1)
or copy the 'Label1' element to that other page.Zestful
P
14

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

Plebe answered 12/7, 2011 at 20:27 Comment(0)
R
3

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!

Remindful answered 27/4, 2011 at 8:19 Comment(0)
Y
1

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!

Yellowstone answered 8/6, 2012 at 15:8 Comment(0)
B
1

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.

Besse answered 2/5, 2013 at 13:13 Comment(0)
P
1

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

AutoGenerateColumns="False"DataKeyNames="ProductID"

instead of

AutoGenerateColumns="False" DataKeyNames="ProductID"
Pharmacology answered 9/5, 2014 at 18:58 Comment(0)
F
1

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.

Foilsman answered 9/11, 2016 at 14:54 Comment(0)
B
1

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

Brosine answered 16/5, 2017 at 14:5 Comment(0)
B
0

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.

Balata answered 5/10, 2012 at 14:38 Comment(0)
J
0

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.

Jestude answered 1/1, 2013 at 6:0 Comment(0)
R
0

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.

Ransell answered 3/1, 2013 at 17:1 Comment(0)
U
0

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.

Urey answered 15/4, 2013 at 18:55 Comment(0)
S
0

I ran into this same issue. Apparently, you shouldn't call a class in the DLL 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.

Seed answered 18/3, 2014 at 15:8 Comment(0)
G
0

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.

Gromyko answered 30/4, 2014 at 20:16 Comment(0)
K
0

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

Kisser answered 5/4, 2018 at 11:8 Comment(0)
V
0

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.

Valentin answered 2/4, 2020 at 16:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.