title: Asp.net code behind not able to access controls in ascx
body: <p>I am fairly new to the .NET scene and have run into a small problem with a simple asp.net app and accessing controls defined on in a .ascx file in the code behind.</p>
  
  <p>Here is my Default.ascx file:</p>
  
  <pre><code>&lt;%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
      CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  
  &lt;asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt;
      &lt;p&gt;
          &lt;asp:Label AssociatedControlID = "userText" AccessKey = "L" runat="server"                       ID="userLabel"&gt;Username&lt;/asp:Label&gt;
          &lt;asp:TextBox ID = "userText" runat="server" MaxLength="20" &gt;&lt;/asp:TextBox&gt;
      &lt;/p&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Here is the Default.aspx.cs file:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class _Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void Page_PreInit(object sender, EventArgs e)
      {   
          userLabel.Text = "Hello!";
      }
  
  }
  </code></pre>
  
  <p>As you can see, pretty straight forward, all I am trying to do is change the text property of a label programmatically. However when I run this I am getting a NullReferenceException, userLabel is not set to an instance of an object. Shouldn't userLabel be a property of my _Default class? I tried explicitly setting userLabel as a property but I get an compile time error saying that userLabel already has a definition. </p>
  
  <p>I imagine I am missing something very simple here but I'm not sure what it is. Could anyone tell me what I am doing wrong? Thanks much! </p>