title: How can I access the controls on my ASP.NET page from a class within the solution?
body: <p>Basically I am not using a MasterPage and I just have a project with a Default.aspx with a few labels, textBoxes etc. I've spent several hours looking for a solution and I have found one, but something in my head still bugs me, that I didn't do it the way I wanted to.</p>
  
  <p>I have no troubles accessing them and setting their properties from the PageLoad.</p>
  
  <p>But I made a class, just like the code snippet below, and it throws me an <strong>"Object reference not set to an instance of an object"</strong>. And I cannot seem to figure out what I am missing.</p>
  
  <pre><code>   class Functions 
      {
         public static void myMethod()
         {
              WebForm1 mainForm = new WebForm1();
              mainForm.myTextBox.Text = "Something.";
         }
      }
  </code></pre>
  
  <p>I managed to do with like this:</p>
  
  <pre><code>   class Functions 
      {
         public static void myMethod()
         {
             System.Web.UI.Page myMainForm;
             myMainForm = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
             TextBox myTextBox = (TextBox)myMainForm.FindControl("myTextBox");
         }
      }
  </code></pre>
  
  <p>But it's not about just doing it, it's about <strong>learning where I am wrong</strong> as I cannot figure it out and I want a simpler way to do it, considering I am very new to ASP.NET. Normally when I am coding basic applications in WinForms - I don't have problems accessing different forms.</p>
  
  <p>And I don't like doing a <strong>FindControl()</strong> for every control I am trying to access. If there is a <em>simple and yet effective</em> workaround I would love to hear it.</p>
  
  <p>Thank you in advance.</p>
  
  <p><strong>Edit</strong>: I've made this example aside from my project, but it's the idea I am trying to accomplish. There might be some small errors in the code snippets, please excuse me.</p>