C# 当前上下文中不存在名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19027025/
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
Name does not exist in the current context
提问by Ortund
So, I'm working on this project between my laptop and my desktop.
所以,我正在我的笔记本电脑和台式机之间开展这个项目。
The project works on the laptop, but now having copied the updated source code onto the desktop, I have over 500 errors in the project, all of them are...
该项目在笔记本电脑上运行,但现在将更新的源代码复制到桌面上,我在项目中有 500 多个错误,所有这些错误都是......
The name does not exist in the current context
该名称在当前上下文中不存在
Here's one example...
这是一个例子...
Jobs.aspx
工作.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Members/Members.master" AutoEventWireup="true" CodeFile="Jobs.aspx.cs" Inherits="Members_Jobs" %>
<%@ Register Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" TagPrefix="aj" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel runat="server" ID="upJobs">
<ContentTemplate>
<!-- page content goes here -->
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Jobs.aspx.cs
工作.aspx.cs
public partial class Members_Jobs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadJobs();
gvItems.Visible = false;
loadComplexes();
loadBusinesses();
loadSubcontractors();
loadInsurers();
pnlCallback.Visible = false;
pnlInsurer.Visible = false;
}
}
// more goes down here
}
Here's a sample of the designer.cs file...
这是designer.cs 文件的示例...
namespace stman.Members {
public partial class Jobs {
/// <summary>
/// upJobs control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.UpdatePanel upJobs;
}
}
I know this error means that the control being referenced generally doesn't exist or is not a part of the class that's referencing it, but as far as I can see, that isn't the case here.
我知道这个错误意味着被引用的控件通常不存在或者不是引用它的类的一部分,但据我所知,这里不是这种情况。
Can anyone provide some insight?
任何人都可以提供一些见解吗?
采纳答案by Secret Squirrel
Jobs.aspx
工作.aspx
This is the phyiscal file -> CodeFile="Jobs.aspx.cs"
这是物理文件 -> CodeFile="Jobs.aspx.cs"
This is the class which handles the events of the page -> Inherits="Members_Jobs"
这是处理页面事件的类 -> Inherits="Members_Jobs"
Jobs.aspx.cs
工作.aspx.cs
This is the partialclass which manages the page events -> public partial class Members_Jobs : System.Web.UI.Page
这是管理页面事件的部分类 - >public partial class Members_Jobs : System.Web.UI.Page
The other part of the partialclass should be -> public partial class Members_Jobs
this is usually the designer file.
部分类的另一部分应该是 ->public partial class Members_Jobs
这通常是设计器文件。
you dont need to have partial classes and could declare your controls all in 1 class and not have a designer file.
您不需要具有部分类,并且可以在 1 个类中声明您的所有控件并且没有设计器文件。
EDIT27/09/2013 11:37
编辑27/09/2013 11:37
if you are still having issues with this I would do as Bharadwaj suggested and delete the designer file. You can then right-click on the page, in the solution explorer, and there is an option, something like "Convert to Web Application", which will regenerate your designer file
如果您仍然遇到此问题,我会按照 Bharadwaj 的建议进行操作并删除设计器文件。然后,您可以在解决方案资源管理器中的页面上右键单击,然后有一个选项,例如“转换为 Web 应用程序”,它将重新生成您的设计器文件
回答by user2776839
I came across a similar problem with a meta tag. In the designer.cs
, the control was defined as:
我遇到了与元标记类似的问题。在 中designer.cs
,控件定义为:
protected global::System.Web.UI.HtmlControl.HtmlGenericControl metatag;
I had to move the definition to the .aspx.cs
file and define as:
我不得不将定义移动到.aspx.cs
文件并定义为:
protected global::System.Web.UI.HtmlControl.HtmlMeta metatag;
回答by TomDestry
"The project works on the laptop, but now having copied the updated source code onto the desktop ..."
“该项目在笔记本电脑上运行,但现在已将更新的源代码复制到桌面上......”
I did something similar, creating two versions of a project and copying files between them. It gave me the same error.
我做了类似的事情,创建了一个项目的两个版本并在它们之间复制文件。它给了我同样的错误。
My solution was to go into the project file, where I discovered that what had looked like this:
我的解决方案是进入项目文件,在那里我发现看起来像这样:
<Compile Include="App_Code\Common\Pair.cs" />
<Compile Include="App_Code\Common\QueryCommand.cs" />
Now looked like this:
现在看起来像这样:
<Content Include="App_Code\Common\Pair.cs">
<SubType>Code</SubType>
</Content>
<Content Include="App_Code\Common\QueryCommand.cs">
<SubType>Code</SubType>
</Content>
When I changed them back, Visual Studio was happy again.
当我把它们改回来时,Visual Studio 又高兴起来了。
回答by cars4chimps
From the MSDN website:
从 MSDN 网站:
This error frequently occurs if you declare a variable in a loop or a try or if block and then attempt to access it from an enclosing code block or a separate code block.
如果您在循环或 try 或 if 块中声明变量,然后尝试从封闭的代码块或单独的代码块访问它,则经常发生此错误。
So declare the variable outside the block.
所以在块外声明变量。
回答by TheDanMan
In case someone being a beginner who tried all of the above and still didn't manage to get the project to work. Check your namespace. In an instance where you copy code from one project to another and you forget to change the namespace of the project then it will also give you this error.
以防万一有人尝试了上述所有方法但仍然无法使项目正常工作。检查您的命名空间。如果您将代码从一个项目复制到另一个项目,而您忘记更改项目的命名空间,那么它也会给您这个错误。
Hope it helps someone.
希望它可以帮助某人。
回答by Jroonk
I solved mine by using an Import directive under my Page directive. You may also want to add the namespace to your Inherits attribute of your Page directive.
我通过在我的页面指令下使用导入指令解决了我的问题。您可能还想将命名空间添加到 Page 指令的 Inherits 属性中。
Since it appears that your default namespace is "stman.Members", try:
<%@ Page Title="" Language="C#" MasterPageFile="~/Members/Members.master" AutoEventWireup="true" CodeFile="Jobs.aspx.cs" Inherits="stman.Members.Members_Jobs" %>
<%@ Import Namespace="stman.Members"%>
由于您的默认命名空间似乎是“stman.Members”,请尝试:
<%@ Page Title="" Language="C#" MasterPageFile="~/Members/Members.master" AutoEventWireup="true" CodeFile="Jobs.aspx.cs" Inherits="stman.Members.Members_Jobs" %>
<%@ Import Namespace="stman.Members"%>
Additionally, data that I wanted to pass between aspx.cs and aspx, I put inside a static class inside the namespace. That static class was available to move data around in the name space and no longer has a "not in context" error.
此外,我想在 aspx.cs 和 aspx 之间传递的数据,我放在命名空间内的静态类中。该静态类可用于在名称空间中移动数据,并且不再出现“不在上下文中”错误。
回答by tno2007
I had this problem in my main project when I referenced a dll file.
当我引用一个 dll 文件时,我在我的主项目中遇到了这个问题。
The problem was that the main project that referenced the dll was targeting a lower framework version than that of the dll.
问题在于,引用 dll 的主项目的目标框架版本低于 dll。
So I upped my target framework version (Right-click project -> Application -> Target framework) and the error disappeared.
所以我提高了我的目标框架版本(右键单击项目 -> 应用程序 -> 目标框架),错误消失了。
回答by Aarthy
I also faced a similar issue. The reason was that I had the changes done in the .aspx page but not the designer page and hence I got the mentioned error. When the reference was created in the designer page I was able to build the solution.
我也遇到了类似的问题。原因是我在 .aspx 页面而不是设计器页面中完成了更改,因此我得到了提到的错误。在设计器页面中创建参考时,我能够构建解决方案。
回答by Richard A. Palacios G.
I also faced a similar issue, the problem was the form was inside a folder and the file .aspx.designer.cs
I had the namespace referencing specifically to that directory; which caused the error to appear in several components:
我也遇到了类似的问题,问题是表单在一个文件夹中,而.aspx.designer.cs
我的命名空间中的文件专门引用了该目录;这导致错误出现在几个组件中:
El nombre no existe en el contexto actual
El nombre no existe en el contexto actual
This in your case, a possible solution is to leave the namespaceline of the Members_Jobs.aspx.designer.cs
file specified globally, ie change this
在您的情况下,可能的解决方案是保留全局指定的文件的命名空间行Members_Jobs.aspx.designer.cs
,即更改此
namespace stman.Members {
namespace stman.Members {
For this
为了这
namespace stman {
namespace stman {
It's what helped me solve the problem.
这就是帮助我解决问题的原因。
I hope to be helpful
我希望能有所帮助
回答by Krishn Pal Chauhan
Same issue happened with me when i mistakenly added some text in front of the top directive of the master page all the controls across all the page included in my project started showing, does not exist in the current context. After spending much time across the forum and self analysis, i found the mistakenly added text and removed but it does not solved the problem. Then i had to click on each error to open the associated page so that VS can load the page and recognize it, once the code was loaded in VS, error from those particular page started disappearing because VS recognized it. clicking on each error, loading each page in VS editor did the trick for me and you have to do it only once because later it will be automatically recognized by VS. I would say it is a Visual Studio Glitch.
同样的问题发生在我身上,当我错误地在母版页的顶部指令前面添加了一些文本时,我的项目中包含的所有页面上的所有控件开始显示,在当前上下文中不存在。在论坛上花了很多时间和自我分析后,我发现错误添加的文本并删除了但并没有解决问题。然后我不得不点击每个错误来打开相关的页面,以便 VS 可以加载页面并识别它,一旦代码在 VS 中加载,来自那些特定页面的错误开始消失,因为 VS 识别了它。单击每个错误,在 VS 编辑器中加载每个页面对我来说都是成功的,你只需要做一次,因为稍后它会被 VS 自动识别。我会说这是一个 Visual Studio 故障。