简体   繁体   English

当前上下文中不存在名称

[英]Name does not exist in the current context

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 Jobs.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.我知道这个错误意味着被引用的控件通常不存在或者不是引用它的 class 的一部分,但据我所知,情况并非如此。

Can anyone provide some insight?谁能提供一些见解?

VS2012 截图

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.希望它可以帮助某人。

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 partial class 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 partial class 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 个类中声明您的所有控件并且没有设计器文件。

EDIT 27/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.如果您仍然遇到此问题,我会按照 Bharadwaj 的建议进行操作并删除设计器文件。 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然后,您可以在解决方案资源管理器中的页面上右键单击,然后有一个选项,例如“转换为 Web 应用程序”,它将重新生成您的设计器文件

"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 又高兴起来了。

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.所以在块外声明变量。

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.所以我提高了我的目标框架版本(右键单击项目 -> 应用程序 -> 目标框架),错误消失了。

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;

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;我也遇到了类似的问题,问题是表单在一个文件夹中,而文件.aspx.designer.cs我有专门引用该目录的命名空间; which caused the error to appear in several components:这导致错误出现在几个组件中:

El nombre no existe en el contexto actual

This in your case, a possible solution is to leave the namespace line of the Members_Jobs.aspx.designer.cs file specified globally, ie change this这在您的情况下,可能的解决方案是保留全局指定的Members_Jobs.aspx.designer.cs文件的命名空间行,即更改此

namespace stman.Members {

For this为此

namespace stman {

It's what helped me solve the problem.这就是帮助我解决问题的原因。

I hope to be helpful我希望能有所帮助

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.此外,我想在 aspx.cs 和 aspx 之间传递的数据,我放在命名空间内的静态类中。 That static class was available to move data around in the name space and no longer has a "not in context" error.该静态类可用于在名称空间中移动数据,并且不再出现“不在上下文中”错误。

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.原因是我在 .aspx 页面而不是设计器页面中完成了更改,因此我得到了提到的错误。 When the reference was created in the designer page I was able to build the solution.在设计器页面中创建参考时,我能够构建解决方案。

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.然后我必须点击每个错误来打开相关的页面,以便 VS 可以加载页面并识别它,一旦代码在 VS 中加载,来自那些特定页面的错误开始消失,因为 VS 识别了它。 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.单击每个错误,在 VS 编辑器中加载每个页面对我来说都是成功的,你只需要做一次,因为稍后它会被 VS 自动识别。 I would say it is a Visual Studio Glitch.我会说这是一个 Visual Studio 故障。

In our case, beside changing ToolsVersion from 14.0 to 15.0 on .csproj projet file, as stated by Dominik Litschauer , we also had to install an updated version of MSBuild, since compilation is being triggered by a Jenkins job.在我们的例子中,除了将 .csproj projet 文件上的 ToolsVersion 从 14.0 更改为 15.0 之外,如Dominik Litschauer 所述,我们还必须安装更新版本的 MSBuild,因为编译是由 Jenkins 作业触发的。 After installing Build Tools for Visual Studio 2019, we had got MsBuild version 16.0 and all new C# features compiled ok.安装 Visual Studio 2019 的构建工具后,我们获得了 MsBuild 16.0 版,并且所有新的 C# 功能都可以正常编译。

I had this come up and found that a couple files used for debugging shared the same CodeFile and Inherits values.我遇到了这个问题,发现用于调试的几个文件共享相同的 CodeFile 和 Inherits 值。 Changing those to be unique resolved my issue.将它们更改为唯一解决了我的问题。

I had the same issue, it solved when I added ".forms" at the end of the namespace in myform.aspx.cs .我遇到了同样的问题,当我在 myform.aspx.cs 的命名空间末尾添加“.forms”时它解决了。 Like this:像这样:
namespace YourProgramName.forms命名空间 YourProgramName.forms

The very first step in these situations is to try build -> clean solution在这些情况下,第一步是尝试build -> clean solution

Sometimes, VS will confuse itself from its own project outputs.有时,VS 会将自己与自己的项目输出混淆。

Clean solution removes the code that VS has previously built . Clean 解决方案删除了​​ VS之前构建的代码。

It doesn't remove the bin/obj directories themselves, rather the output files in these folders.它不会删除 bin/obj 目录本身,而是删除这些文件夹中的输出文件。

In my case, somehow the *.csproj file of my main project lost the reference to a secondary project it depends on, it probably happened during the import phase in Visual Studio 2022 (from VS 2017).就我而言,不知何故,我的主项目的 *.csproj 文件丢失了对它所依赖的辅助项目的引用,它可能发生在 Visual Studio 2022 的导入阶段(来自 VS 2017)。 I checked the backup copy of the *.csproj file and there was an entire tag missing.我检查了 *.csproj 文件的备份副本,并且缺少一个完整的标签。 I manually copied it over from the backup, and the error disappeared.我从备份中手动复制了它,错误消失了。

  <ItemGroup>
    <ProjectReference Include="..\projectName\projectName.csproj">
        <Project>{cf9ab6e2-8e15-4915-820d-2d0d8f365cbf}</Project>
        <Name>ProjectName</Name>
    </ProjectReference>     
  </ItemGroup>

I also face the same problem and project showed scores of errors.我也面临同样的问题,项目显示出许多错误。 I copy some dll's which are creating problem from my colleague and copy them in another folder then delete those dll files from my project and add their references again from new location and it solved the problem.我从我的同事那里复制了一些造成问题的 dll 并将它们复制到另一个文件夹中,然后从我的项目中删除那些 dll 文件并从新位置再次添加它们的引用,它解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM