views:

26

answers:

4

This has been working for the past three weeks that I've been developing this application, and for some reason has decided to stop working five minutes ago despite my apparently not doing anything.

In Default.aspx:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html"; charset=utf-8 />
    <title></title>

    <script>(A bunch of script declarations, etc)

</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

In default.aspx.cs:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Web.UI.HtmlControls.HtmlGenericControl g = new System.Web.UI.HtmlControls.HtmlGenericControl();

        //(Dynamically builds a load of html to insert into the form)

        form1.Controls.Add(g);
    }
}

I'm suddenly getting the compiler error 'The name 'form1' does not exist in the current context' despite apparently not having made any changes to either of these files. Intellisense can't seem to see it anywhere, from any scope within the file, either.

Does anyone have any idea why?

A: 

I think this public partial class Default: System.Web.UI.Page

should be public partial class AppName.Default : System.Web.UI.Page

or your code behind have namespace? like..

namespace AppName
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.UI.HtmlControls.HtmlGenericControl g = new System.Web.UI.HtmlControls.HtmlGenericControl();

            //(Dynamically builds a load of html to insert into the form)

            form1.Controls.Add(g);
        }
    }
}
Muhammad Akhtar
+1  A: 

check that form1 exists in the designer file (default.aspx.designer.cs) as i suspect its missing

PaulStack
+1  A: 

In the Build menu in VS, try to run "Clean Solution" and then Build.

awe
A: 

Problem not having runat="Server" in the head declaration, which I must have changed about two weeks ago and visual studio has somehow managed to not have a problem with it until today despite several complete rebuilds of the project. Typical Microsoft.

Toby Wilson