Label doesnt exist in the current context

佐手、 提交于 2019-12-18 08:55:35

问题


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.


回答1:


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:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ajaxTesting._Default" %>



回答2:


The Best possible solution would be:

  1. Copy all the lines of code between <body> and </body>
  2. Create a new aspx page in your project.
  3. Replace the <body> tag with the one you copied
  4. Similarly copy and paste the code from .cs file also.

I feel this should solve your problem.




回答3:


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




回答4:


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.




回答5:


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!




回答6:


I faced this same problem in asp.net website (3.5) In my case there were 2 copies of the same file.

  1. Compute.aspx
  2. Compute_backup.aspx

I excluded the #2 file from the website and it worked for me.




回答7:


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.



来源:https://stackoverflow.com/questions/6008588/label-doesnt-exist-in-the-current-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!