Asp.net从aspx的文本框中获取值,以进行背后的代码 [英] Asp.net get value from Textbox in aspx to code behind

查看:55
本文介绍了Asp.net从aspx的文本框中获取值,以进行背后的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net和C#编程语言创建一个登录系统.后面处理用户和密码的代码已完成.但是在视图层中,我很难从用户名文本框和密码文本框中获取值,并将其传递给代码隐藏.

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.

两个文本框都是ID标识的,而在我的一些编程技能中,ID应该足以访问元素.

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

这是我的aspx登录页面:

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. 使用代码:

  1. Using the code:

string user = this.UserName.Text;
string pass = this.Password.Text;

  • 使用代码:

  • Using the code:

    Textbox UserName = this.FindControl("UserName");
    

  • 删除了 aspx.design.cs 并右键单击该表单并将其转换为应用程序;

  • 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;
    

    它给我抛出一个错误:

    对象引用未设置对象的实例.

    Object Reference not set an instance of an object.

    您能建议解决我的问题吗?

    Can you suggest any solution to my problem?

    推荐答案

    这是因为这些控件是模板的一部分.它们不是直接在页面上,而是在初始化 Login 控件时动态添加到页面中.要访问它们,您需要 FindControl :

    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;
    

    这篇关于Asp.net从aspx的文本框中获取值,以进行背后的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆