skip to Main Content

I have a apsx with the following code:

<div class="form-group col-6 mx-auto">
    <asp:TextBox CssClass="form-control" name="username_box" ID="username_box"
         runat="server" placeholder="Username" TextMode="SingleLine"></asp:TextBox>
    <br />
</div>

<div class="form-group col-6 mx-auto">
    <asp:TextBox CssClass="form-control" ID="password_box" runat="server"
         placeholder="Password" TextMode="Password"> </asp:TextBox>
    <br /> 
</div>

<div class="form-group d-grid gap-2 col-6 mx-auto">
    <asp:LinkButton class="btn btn-danger btn-block" ID="login_button" runat="server"
         text="Login" OnClick="Loginbutton_Click"></asp:LinkButton>
    <br />
</div>

When a user enters the username in the first TextBox (ID:username_box) and enters the password on button click the Code Behind should capture the username_box.Text value. However it always return null. I am not sure what I am doing wrong, please your assistance.

See code behind.

protected void Loginbutton_Click(object sender, EventArgs e)
{
    username = username_box.Text;
    Response.Write(username);
}

I am expecting the entered value from the username_box.Text property however I receive nothing.
I looked as IsPostBack but I am not sure I am looking at it properly.

2

Answers


  1. Chosen as BEST ANSWER

    I found the error that was made. There was another form within the main form on the Site1.Master page. Further reading indicated that is not allowed.

    I removed the nested form and the values were retrieved. Thank you for your support.


  2. You may use:

    protected void Loginbutton_Click(object sender, EventArgs e)
    {
    username = username_box.Value //OR username = Request.Form["username_box"]
    //your code...
    }
    

    https://www.aspsnippets.com/Articles/Get-value-of-HTML-Input-TextBox-in-ASPNet-code-behind-using-C-and-VBNet.aspx

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search