views:

176

answers:

3

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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?

+3  A: 

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.

womp
+1  A: 

Well, your example code is missing the most important thing for an ASP.NET page, which is the @Page directive that tells the ASP.NET engine what class to instantiate for the page, and if it needs to dynamically compile any code.

Bryan
Yea, thanks. Just forgot to add it. I have it and it still doesn't work
abatishchev
+1  A: 

Make sure you didn't just copy the entire page from your web site to your web application, but you created a new page.

Make sure your .aspx page has a correct @page directive, something close to:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
AaronS
It seems that my problem comes exactly from directly copying page from site to app. Thanks!
abatishchev