views:

3786

answers:

4

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.

+1  A: 

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

CMS
+1  A: 

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

mosheb
or copy the 'Label1' element to that other page.
mathijsuitmegen
A: 

This didnt fix my problem. I am having the same problems, however, the CodeBehind and the Inherits are the same. Also, there are no older pages or other pages that are using the same partial classes.

I inherited the code from someone and I am assuming that it was working but it doesnt have a .designer.cs file on any of the pages. The version used was VS 2005 with .NET 2.0.

Any suggestions?

Pritika
+4  A: 

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.

RobotNerd
fyi the right click "Convert to Web Application" option is also available on folders and projects.