Toby Wilson
Toby Wilson

Reputation: 1507

ASP.NET: Suddenly the name 'form1' does not exist in the current context?

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

<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?

Upvotes: 0

Views: 2311

Answers (3)

Toby Wilson
Toby Wilson

Reputation: 1507

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.

Upvotes: 0

awe
awe

Reputation: 22462

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

Upvotes: 1

stack72
stack72

Reputation: 8288

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

Upvotes: 1

Related Questions