3

I was given a webproject written with aspx/c#. When I loaded it into Visual Studio 2010, I got many error telling me that some controls in the code-behind files do not exist in the current context.

I checked for the common pitfalls, like wrong code-behind file name, missing runat-attribute, restart VS, reload project, yet nothing resolves the error.

What else can I do to check where the problem is?

F.P
  • 17,421
  • 34
  • 123
  • 189
  • 1
    I can't even build it because of the errors – F.P Oct 18 '12 at 09:31
  • 1
    similar question and possible solution here: http://stackoverflow.com/questions/38670/asp-net-controls-cannot-be-referenced-in-code-behind-in-visual-studio-2008 – 03Usr Oct 18 '12 at 10:12
  • thanks @03Usr, the second answer to that question was the solution. i deleted all designer-files and let them regenerate, now it works. – F.P Oct 18 '12 at 10:48

5 Answers5

5

Try to change the code-behinde to CodeFile="where code locate", then it probably will work.

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
Bashar Abu Shamaa
  • 1,998
  • 2
  • 21
  • 36
  • I did this and the codebehind was able to re-register the cs file. I just did exactly what you stated, deleted what was there and the quotes, then re-typed the quotes and selected the cs file and the problem was fixed. – Anthony Tristan Apr 16 '19 at 15:30
  • If I re-type the quotes and try to pick the file, all the code files in the project aren't there!! Yet they're under the aspx files in the Solution Explorer!! – SteveCav Jul 28 '20 at 06:50
2

inherit class for the markup file and code behind file should match ,and also make sure if these controls are not third party and may need referencing their dlls or something .If these controls are user controls(ascx) controls , make sure they are using the correct tagname in the register line at the top .

Pirate
  • 71
  • 1
  • 10
  • inherit class/namespace is correct. they are all simple ` – F.P Oct 18 '12 at 09:28
  • I meant if you have any user control registration declaration like this (at the top of markup file) <%@ Register TagPrefix="xxx" TagName="tagname" Src="Controls/usercontrolnamefile.ascx" %> – Pirate Oct 19 '12 at 23:56
1

Cudos to @03Usr the following link brought the answer:

ASP.NET controls cannot be referenced in code-behind in Visual Studio 2008

Deleting all designer files and converting the project als web application brought the designer files back, effectively making the controls referenceable again.

Community
  • 1
  • 1
F.P
  • 17,421
  • 34
  • 123
  • 189
1

This is a really annoying hack but if you delete the control from the aspx page and then save the page, then paste the control back onto the page and save again the code behind then seems to recognize the control. I keep running into this problem whenever I change an attribute like the ID.

Hunter Nelson
  • 1,707
  • 3
  • 20
  • 37
  • This is messed up. I did this on three, it only worked on one. VS 2019. I like Visual Studio, but I guess I should wait a full five years before moving to the next version. – Steve Jan 13 '22 at 20:49
  • What's funny, is, you know it knows it's there because it shows up in intellisense. – Steve Jan 13 '22 at 20:50
1

In my case, the aspx file was inside a folder named XYZ, and the class name was ABC. So ASP named the C# file's class name as XYZ_ABC After renaming the file to ABC, the error was fixed

Hope it helps!

Ashwin Balani
  • 745
  • 7
  • 19