Question

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.

Was it helpful?

Solution

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" %>

OTHER TIPS

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.

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

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.

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!

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top