title: Asp.net get value from Textbox in aspx to code behind
body: <p>I'm creating a login system in asp.net and C# programming language. The code behind to handle the user and password is done. But in view layer, I'm troubling to get the values from username textbox and password textbox and passing it to codebehind.</p>
  
  <p>Both textboxes are ID identified and in my few skills of programming, an ID should be enough to access the elements.</p>
  
  <p>This is my aspx login page:</p>
  
  <pre><code> &lt;asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false"&gt;
          &lt;LayoutTemplate&gt;
              &lt;p class="validation-summary-errors"&gt;
                  &lt;asp:Literal runat="server" ID="FailureText" /&gt;
              &lt;/p&gt;
              &lt;fieldset&gt;
                  &lt;legend&gt;Log in Form&lt;/legend&gt;
                  &lt;ol&gt;
                      &lt;li&gt;
                          &lt;asp:Label ID="Label1" runat="server" AssociatedControlID="UserName"&gt;User name&lt;/asp:Label&gt;
                          &lt;asp:TextBox runat="server" ID="UserName" /&gt;
                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." /&gt;
                      &lt;/li&gt;
                      &lt;li&gt;
                          &lt;asp:Label ID="Label2" runat="server" AssociatedControlID="Password"&gt;Password&lt;/asp:Label&gt;
                          &lt;asp:TextBox runat="server" ID="Password" TextMode="Password" /&gt;
                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." /&gt;
                      &lt;/li&gt;
                      &lt;li&gt;
                          &lt;asp:CheckBox runat="server" ID="RememberMe" /&gt;
                          &lt;asp:Label ID="Label3" runat="server" AssociatedControlID="RememberMe" CssClass="checkbox"&gt;Remember me?&lt;/asp:Label&gt;
                      &lt;/li&gt;
                  &lt;/ol&gt;
                  &lt;asp:Button ID="Button1" runat="server" CommandName="Login" Text="Log in"  OnClick="Button1_Click"/&gt;
              &lt;/fieldset&gt;
          &lt;/LayoutTemplate&gt;
      &lt;/asp:Login&gt;
  </code></pre>
  
  <p>This I did do get values from UserName and Password Textboxes:</p>
  
  <ol>
  <li><p>Using the code:</p>
  
  <pre><code>string user = this.UserName.Text;
  string pass = this.Password.Text;
  </code></pre></li>
  <li><p>Using the code:</p>
  
  <pre><code>Textbox UserName = this.FindControl("UserName");
  </code></pre></li>
  <li><p>Deleted the <code>aspx.design.cs</code> and right click on the form and Convert it to application;</p></li>
  <li><p>In the designer, add the following lines of code:</p>
  
  <pre><code>protected global::System.Web.UI.WebControls.TextBox UserName;
  protected global::System.Web.UI.WebControls.TextBox Password;
  </code></pre></li>
  </ol>
  
  <p>Nothing worked so far, and when I reach this line:</p>
  
  <pre><code>string user = this.UserName.Text;
  </code></pre>
  
  <p>It throws me an error:</p>
  
  <blockquote>
    <p>Object Reference not set an instance of an object.</p>
  </blockquote>
  
  <p>Can you suggest any solution to my problem?</p>