简体   繁体   中英

The name does not exist in the current context

I have the following HTML in an .aspx:

 <div ID="divText" runat="server" style="position:absolute;top:60px; left:800px; width:600px; height:100px; z-index:2;font-size:200%">
 </div>

Then in the code behind, IntelliSense finds the 'divText' but I get the compile error listed in the title

 string productEdition = ConfigurationManager.AppSettings["Club"];
 divText.InnerHtml = productEdition;

The compile error:

Error 3 The name 'divText' does not exist in the current context

The thing is the same EXACT html and code work in another file. We have tried everything. Any ideas?

This error usually occours when you have copy pasted an aspx file. Please verify that your aspx is pointing to the correct cs file. Also check the names of the two files.

Other option is that there is something wrong with your designer file. If you want to Visual studio to regenerate your designer.cs file, you can go into design mode, make a small change and save the file.

Let me know if none of this works (90% of the time this issue is caused by this).

如果其他建议不起作用,请删除设计器文件,右键单击标记文件,然后单击“转换为 Web 应用程序”。

I had a similar issue when getting old VS2005 aspx/aspx.cs files into a new project in VS2013. I resolved this by creating a new web application, creating a new webform for each aspx page and copying the code for .aspx and .cs. Once copied I have to change the .cs files first line to use CodeBehind instead of CodeFile .

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="_Login" %>

TO

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="_Login" %>

I hope this helps someone else.

Most likely the control variable did not get added to the aspx.designer.cs file. You can re-add the control and Visual studio might add it for you or you can add it manually to the designer.

protected global::System.Web.UI.HtmlControls.HtmlGenericControl divText;

Right-click on the ASPX (or ascx) file, and select Convert to web application (or something like that). That will force a refresh on the designer file.

and you can right-click on project in solution explorer and select to web application. in this case all of your project files will be change.

if you can't see convert to web application after right click, you can click on project menu and you see convert to web application in bottom

I think the problem mix of 3 issue in the middle:

  1. check if namespace in the code MyFile.aspx.cs file is the same of project.
  2. in the MyFile.design.aspx.cs the namespace should match with previous file
  3. if the design file of your aspx page in the partial class have declared the control that give that error.

In my case the issue appeared after copy/added the existing aspx file. After checked if all namespace are correct the issue was solved.

Looks like your problem may be the uppercase ID which is not setting the id of your element. Also not sure how the id becomes an object given the code you posted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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