Search code examples
asp.netservercontrols

ASP .Net runat server controls not compiling


I have created a new Web project (.Net 3.5) and I've removed the original Default page in favour of creating a Master page and then recreating a Default with Master page. All worked lovely. Now, when I add my controls inside of the ContentPlaceHolder on the Default page, I get compiler errors when I try to access the controls from the CodeFile:

Error 4 '_Default' does not contain a definition for 'FirstName' and no extension method 'FirstName' accepting a first argument of type '_Default' could be found (are you missing a using directive or an assembly reference?)

My declaration of the control:

<asp:TextBox runat="server" ID="FirstName" />

Any ideas?

EDIT

Page declaration:

<%@ Page Title="" Language="C#" MasterPageFile="~/Master/MasterPage.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

Code-behind:

string firstname = FirstName.Text;

Solution

  • I managed to get this working by changing CodeFile to CodeBehind.

    Working:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Master/MasterPage.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %>

    Not working:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Master/MasterPage.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>