简体   繁体   English

Asp.net从aspx的文本框中获取值以进行代码隐藏

[英]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文本框中获取值:

  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; 删除了aspx.design.cs并右键单击表单并将其转换为应用程序;

  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? 您能建议解决我的问题吗?

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.

相关问题 从代码隐藏在 ASP.NET aspx 中绑定 TextBox - Bind TextBox in ASP.NET aspx from Code Behind 从ASP.NET中的代码获取附加控件值 - Get append control value from code behind in ASP.NET javascript ASP.NET hiddenfield 从后面的代码中获取价值 - javascript ASP.NET hiddenfield get value from code behind 如何在asp.net代码后面获取引导文本框值并在存储过程中作为参数发送? - how to get bootstrap textbox value in asp.net code behind and send as parameters in store procedure? 如何通过ASP.NET代码隐藏获取放置在转发器中的html文本框的值? - How to get the value of an html textbox placed in a repeater, through ASP.NET code-behind? ASP.NET TextBox Control - 获取代码后面的默认文本值? - ASP.NET TextBox Control - Get the default text value in code behind? 在aspx.cs(在后面的代码)asp.net中创建RequiredFieldValidator - Make RequiredFieldValidator in aspx.cs (code behind) asp.net 在后面的代码中无法识别 aspx 元素 (asp.net) - aspx element is not recognized in code behind (asp.net) ASP.Net单击以获取代码背后的HTML单元格值 - ASP.Net Get HTML cell value for code behind by clicking 将javascript变量值获取到Asp.net背后的代码 - get the javascript variable value to Code Behind Asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM