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

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.

Leave a Comment