Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

c# - Asp.net get value from Textbox in aspx to code behind

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:

  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;

  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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...