Question

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.

Both textboxes are ID identified and in my few skills of programming, an ID should be enough to access the elements.

This is my aspx login page:

 <asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
        <LayoutTemplate>
            <p class="validation-summary-errors">
                <asp:Literal runat="server" ID="FailureText" />
            </p>
            <fieldset>
                <legend>Log in Form</legend>
                <ol>
                    <li>
                        <asp:Label ID="Label1" runat="server" AssociatedControlID="UserName">User name</asp:Label>
                        <asp:TextBox runat="server" ID="UserName" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                    </li>
                    <li>
                        <asp:Label ID="Label2" runat="server" AssociatedControlID="Password">Password</asp:Label>
                        <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                    </li>
                    <li>
                        <asp:CheckBox runat="server" ID="RememberMe" />
                        <asp:Label ID="Label3" runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
                    </li>
                </ol>
                <asp:Button ID="Button1" runat="server" CommandName="Login" Text="Log in"  OnClick="Button1_Click"/>
            </fieldset>
        </LayoutTemplate>
    </asp:Login>

This I did do get values from UserName and Password Textboxes:

  1. Using the code:

    string user = this.UserName.Text;
    string pass = this.Password.Text;
    
  2. Using the code:

    Textbox UserName = this.FindControl("UserName");
    
  3. Deleted the aspx.design.cs and right click on the form and Convert it to application;

  4. In the designer, add the following lines of code:

    protected global::System.Web.UI.WebControls.TextBox UserName;
    protected global::System.Web.UI.WebControls.TextBox Password;
    

Nothing worked so far, and when I reach this line:

string user = this.UserName.Text;

It throws me an error:

Object Reference not set an instance of an object.

Can you suggest any solution to my problem?

Was it helpful?

Solution

This is because these controls are parts of a template. They are not directly on the page, they are added there dynamically when Login control is initialized. To access them you need FindControl:

string user = ((TextBox)Login1.FindControl("UserName")).Text;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top