[英]Asp.net get value from Textbox in aspx to code behind
I'm creating a login system in asp.net and C# programming language. 我正在使用asp.net和C#编程语言创建一个登录系统。 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. 两个文本框都是ID标识的,而在我的编程技巧中,ID应该足以访问元素。
This is my aspx login page: 这是我的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>
This I did do get values from UserName and Password Textboxes: 我确实从UserName和Password文本框中获取值:
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; 删除了
aspx.design.cs
并右键单击表单并将其转换为应用程序;
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? 您能建议解决我的问题吗?
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. 它们不是直接在页面上,而是在初始化
Login
控件时动态地添加到页面中。 To access them you need FindControl
: 要访问它们,您需要
FindControl
:
string user = ((TextBox)Login1.FindControl("UserName")).Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.