I created a New Web Site in Visual Studio 2008, added a page with a control on it:
<!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">
<body>
<form runat="server">
<div>
<asp:Label runat="server" ID="lblFoobar">Foobar</asp:Label>
</div>
</form>
</body>
</html>
and generated code-behind class got a new field lblFoobar.
I can access it into any member as a local instance variable. All works fine.
But when I created a New ASP.NET Application the same approach throws an error:
The name 'lblFoobar' does not exist in the current context
Why?
There should be two files attached to your .aspx page. One should be a .aspx.designer.cs file, and the other should be the codebehind file, the .aspx.cs file.
If you right click the aspx file in the solution explorer and choose “View Codegen File”, it should open the .aspx.designer.cs file for you. The Label should be declared in there.
If not, then the designer hasn’t correctly parsed your markup yet. Open the .aspx file in design view, make a small change, revert it and delete it to get the designer to run again.