2

I'm trying to make a web page with a login screen. I have 2 textboxes that are used to type the username and the password. I'm using an AJAX Update Panel, so in case that username doesn't exist (I'm using SQL Server), it makes visible a label that shows the error. The problem is that when the user types in the username textbox, AutoPostBack fires and when you move to the password textbox, you have to click twice in order to start typing in the password textbox.

C# code:

protected void txtUsername_TextChanged(object sender, EventArgs e)
    {
        string msgError = businessLogin.checkUsername(txtUsername.Text);
        if (msgError != "")
        {

            lblWrongUser.Text = msgError;
            lblWrongUser.Visible = true;
            btnLogin.Enabled = false;         
        }
        else
        {
            lblWrongUser.Visible = false;
            btnLogin.Enabled = true;
        }

    }    

ASP.Net:

<asp:TextBox ID="txtUsername" clientidmode="Static" runat="server" OnTextChanged="txtUsername_TextChanged" AutoPostBack="True"></asp:TextBox>
  • In your event handler, you could call `txtPassword.Focus();` when the username is valid, and `txtUsername.Focus();` when it is not. – ConnorsFan Sep 08 '16 at 02:26
  • I think it is a bad practice to validate username/password on every keypress/text changed. It increases overhead on page. You should let user enter the complete username and then check whether its valid or not. But Its always your decision. Also, page my be in the state of postback, when you type in password at first. – Rohit Garg Sep 08 '16 at 04:49

0 Answers0