当前上下文中不存在标签
为什么我收到此错误“名称‘lblHelloWorld’在当前上下文中不存在”?我该如何修复它?
<%@ Page Language="C#" AutoEventWireup="True" Inherits="_Default" Codebehind="Default.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Hello, world!</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="pnlHelloWorld" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblHelloWorld" Text="Click the button!" />
<br /><br />
<asp:Button runat="server" ID="btnHelloWorld" OnClick="btnHelloWorld_Click" Text="Update label!" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ajaxTesting
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnHelloWorld_Click(object sender, EventArgs e)
{
lblHelloWorld.Text = "Hello, world - this is a fresh message from ASP.NET AJAX! The time right now is: " + DateTime.Now.ToLongTimeString();
}
}
}
我尝试过清理和重建;删除designer.cs文件并重新创建它但没有用。
why m i getting this error "The name 'lblHelloWorld' does not exist in the current context"? How do i fix it?
<%@ Page Language="C#" AutoEventWireup="True" Inherits="_Default" Codebehind="Default.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Hello, world!</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="pnlHelloWorld" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblHelloWorld" Text="Click the button!" />
<br /><br />
<asp:Button runat="server" ID="btnHelloWorld" OnClick="btnHelloWorld_Click" Text="Update label!" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ajaxTesting
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnHelloWorld_Click(object sender, EventArgs e)
{
lblHelloWorld.Text = "Hello, world - this is a fresh message from ASP.NET AJAX! The time right now is: " + DateTime.Now.ToLongTimeString();
}
}
}
I tried cleaning and rebuliding; deleting the designer.cs file and recreating it but was of no use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为问题出在您的
designer.cs
文件中。尝试在Designer.cs 文件中手动添加此标签的引用。编辑:
修改代码后的问题出现在
名称空间
中。要解决您的问题::
将您的行替换为:
I think the problem in your
designer.cs
file.Try to add a reference manually for this label in the designer.cs file.EDIT:
The problem after revision to your code is in the
name space
.To fix your problem::
Replace your line by this:
最好的解决方案是:
之间的所有代码行。并
aspx
页面。标记替换为您复制的标记。
.cs
文件中复制并粘贴代码。我觉得这应该可以解决你的问题。
The Best possible solution would be:
<body> and </body>
aspx
page in your project.<body>
tag with the one you copied.cs
file also.I feel this should solve your problem.
第 1 步:
复制 aspx 表单、代码隐藏和设计器文件,然后通过复制和粘贴重新创建表单。
第 2 步:
如果第 1 步未能解决问题:在 Visual Studio 中,转到“编辑”->“编辑”。查找并替换 ->在文件中查找并在项目中搜索同名的控件。
步骤 3
关闭 Visual Studio 和所有浏览器。暂时停止 IIS 并导航到临时 ASP.NET 文件文件夹(该路径可能与您计算机上的路径不同):
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
开始删除站点中的所有内容文件夹位于此处,从底部开始(因为您可能会受到 ACL 文件夹权限限制)。
重新启动 IIS 并重试(在尝试了前面的一个或两个步骤之后)。
Step 1:
Copy off the aspx form, codebehind and designer files and re-create the form again by copying and pasting.
Step 2:
If step 1 doesn't fix it: In visual Studio, go to Edit -> Find and Replace -> Find in Files and search within your project for a control with the same name.
Step 3
Close out Visual Studio and all browsers. Temporarily stop IIS and navigate to the temp ASP.NET files folder (the path may be different on your machine):
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
Start deleting everything from the site folder in here, starting at the bottom (because you may get ACL folder permission restrictions).
Re-start IIS and try again (after you have tried one or both of previous steps).
我的另一种可能性是:
如果您复制 .cs 文件,例如:复制“Default.aspx.cs”,得到“Default.aspx - Copy.cs”(现在您在同一文件夹中拥有这两个文件),
结果将是:该文件将出现在项目文件列表中。即使将复制的文件排除在项目之外,但将文件保留在同一文件夹中也无法解决问题。
为了避免此问题,请将复制的文件从项目文件夹中删除。
Another possibility that I had:
If you duplicate the .cs file, for example: duplicate "Default.aspx.cs", getting "Default.aspx - Copy.cs" (now you have the two files on the same folder),
the result will be: the file will appear in the project file list. Even excluding the copied file out of the project, but leave the file on the same folder will not solve the problem.
In order to avoid this issue, remove the copied file out of the project folders.
我从别人那里导入源文件后遇到了这个问题。一段时间后我发现我没有任何 Designer.cs 文件。
我遵循这个解决方案。特别是关于“转换为Web应用程序”的部分。这对我来说很有效!
I had this problem after importing source files from someone else. After a while I discovered I didn't have any designer.cs file.
I followed this solution. Especially the part about "Convert to Web Application". That did the trick for me!
我在 asp.net 网站 (3.5) 中遇到了同样的问题,在我的例子中,同一文件有 2 个副本。
我从网站中排除了 #2 文件,它对我有用。
I faced this same problem in asp.net website (3.5) In my case there were 2 copies of the same file.
I excluded the #2 file from the website and it worked for me.
我完全删除了 apex 文件并重新创建了它并保留了对象名称 _Default。然后我开始相应地调整 C# 代码,瞧,一切正常。这是在 VS2008 中使用 ASP.NET 3.5 时的情况。在本例中,我让它工作了,但是 VS2008 仍然有一些奇怪的地方,否则就没有必要了。
I completely removed the apex file a re-created it and left to object name _Default. I then set about adjusting the C# code accordingly and voila, it worked OK. This was when working with ASP.NET 3.5 in VS2008. In this case I got it to work, but there is still something funky about VS2008 or this would not be necessary.