C# 当前上下文中不存在名称“controlname”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/706603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 14:11:01  来源:igfitidea点击:

The name 'controlname' does not exist in the current context

c#asp.netvisual-studio-2005code-behind

提问by zohair

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:

我有一个正在开发的 Web 应用程序(ASP.NET2.0 with C#,使用 VS2005)。一切正常,突然间我收到错误消息:

Error 1 The name 'Label1' does not exist in the current context

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.

每次我在页面的代码隐藏中使用控件时,还有 43 种其他类型。

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..

这仅发生在 1 页上。就好像代码隐藏无法识别控件一样。另一个有趣的事情是智能感知也没有接受任何控件。

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.

我试图清理解决方案文件,删除 obj 文件,从项目中排除文件,然后重新添加它们,关闭 VS 并重新启动它,甚至重新启动我的计算机,但这些都不起作用。

采纳答案by CMS

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

检查 @Page 指令上的文件名和 Inherits 属性背后的代码,确保它们都匹配。

回答by mosheb

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

排除引用相同代码隐藏文件的任何其他页面,例如您复制和粘贴的旧页面。

回答by RobotNerd

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
  • 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."

我试图创建一个支持 AJAX 的页面(如果您不熟悉这个,请查看有关使用 ScriptManager 对象的教程)。我尝试通过 C# 代码访问页面中的 HTML 元素,但出现错误,指出 HTML ID 值的标识符“在当前上下文中不存在”。

To solve it, I had to do the following:

为了解决它,我必须执行以下操作:

1. Run at server

1.在服务器上运行

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:

要在 C# 代码中将 HTML 元素作为变量访问,必须将以下值放置在 aspx 文件的 HTML 元素标记中:

runat="server"

Some objects in the Toolbox in the Visual Studio IDE do not automatically include this value when added to the page.

Visual Studio IDE 工具箱中的某些对象在添加到页面时不会自动包含此值。

2. Regenerate the auto-generated C# file:

2. 重新生成自动生成的 C# 文件:

  • 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 designerfile. 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.
  • 在解决方案资源管理器中,在 aspx 文件下应该有两个文件:*.aspx.cs 和 *.aspx.designer.cs。设计器文件是自动生成的。
  • 删除现有的 *.aspx.designer.cs 文件。确保您只删除设计器文件。不要删除另一个,因为它包含页面的 C# 代码。
  • 右键单击父 aspx 文件。在弹出菜单中,选择Convert to Web Application

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

现在应该可以在 C# 代码文件中访问该元素。

回答by Protector one

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!

此外,请确保您没有意外尝试继承或定义与其他文件相同(部分)类的文件。请注意,这些文件似乎与实际出现错误的文件无关!

回答by user735232

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

我有同样的问题。事实证明,我的项目中有“MyPage.aspx”和“MyPage.aspx 的副本”。

回答by Ashley Grenon

I ran into this same error, except it was a WPF error. I was rearranging projects and had a control defined in like this:

我遇到了同样的错误,但它是 WPF 错误。我正在重新安排项目并定义了一个控件,如下所示:

<local:CustomControl Name="Custom" /> 

In my code behind I tried using Custom.Blah, but I got the error:

在我后面的代码中,我尝试使用 Custom.Blah,但出现错误:

The name 'Custom' does not exist in the current context

What did the trick for me was changing my control in Xaml to this:

对我来说,诀窍是将我在 Xaml 中的控件更改为:

<local:CustomControl x:Name="Custom" />

Hope this helps someone out there!

希望这可以帮助那里的人!

回答by user1661290

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.

上面提供的解决方案选项 #2 适用于 windows 窗体应用程序,而不是 web aspx 应用程序。我在 Web 应用程序中遇到了类似的错误,我通过删除一个具有相同名称的用户控件的文件解决了这个问题,这个 aspx 文件实际上是一个备份文件,在过程中的任何地方都没有被引用,但它仍然导致了错误,因为在备份文件上注册的用户控件的名称与流程中引用的 aspx 文件的名称完全相同。所以我删除了备份文件并构建了解决方案,构建成功。

Hope this helps some one in similar scenario.

希望这对类似情况下的某些人有所帮助。

Vijaya Laxmi.

维贾亚·拉克西米。

回答by Desmond

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.

我遇到了同样的问题,因为我试图从 Visual Studio 2010 项目重新生成 aspx 文件,因此控件具有 clientidmode="Static" 属性。当它被删除时,它就解决了。

回答by R?zvan Flavius Panda

I had a similar problem when tweaking with a Repeater after converting it from a DataList.

在从 DataList 转换后使用中继器进行调整时,我遇到了类似的问题。

Problem was that I accidentally united 2 attributes when deleting an unneeded one.

问题是我在删除不需要的属性时不小心合并了 2 个属性。

<asp:Repeater runat="server" ID="ClientsRP"DataSourceID="ClientsDS">
    .
    .
    .
</asp:Repeater>

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

这阻止了设计文件中中继器的生成。

回答by user1985189

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.

原来 Visual Studio 是愚蠢的,因为问题与我制作的 aspx.cs 类的备份文件有关。我删除了那些,错误就消失了。