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:
Using the code:
string user = this.UserName.Text;
string pass = this.Password.Text;
Using the code:
Textbox UserName = this.FindControl("UserName");
Deleted the aspx.design.cs
and right click on the form and Convert it to application;
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…