Sto creando un sistema di login in asp.net e in linguaggio di programmazione C#. Il codice dietro per gestire l'utente e la password è fatto. Ma a livello di vista, mi preoccupo di ottenere i valori dalla casella di testo username e password e passarla a codebehind.Asp.net ottiene valore da Textbox in aspx per code behind
Entrambe le caselle di testo sono identificate ID e nelle mie poche competenze di programmazione, un ID dovrebbe essere sufficiente per accedere agli elementi.
Questa è la mia pagina di login aspx:
<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>
Questo l'ho fatto ottenere i valori da UserName e Password Caselle di testo:
Utilizzando il codice:
string user = this.UserName.Text; string pass = this.Password.Text;
Utilizzando il codice:
Textbox UserName = this.FindControl("UserName");
Eliminato il
aspx.design.cs
e fare clic con il tasto destro sul modulo e convertirlo in applicazione;Nella finestra di progettazione, aggiungere le seguenti righe di codice:
protected global::System.Web.UI.WebControls.TextBox UserName; protected global::System.Web.UI.WebControls.TextBox Password;
niente ha funzionato finora, e quando raggiungo questa linea:
string user = this.UserName.Text;
mi getta un errore :
Object Reference not set an instance of an object.
Puoi suggerire qualche soluzione al mio problema?
Ha funzionato! Molte grazie!! – VCore