0

I have a username TextBox and a Label which should update to (V or X) when the TextBox text is changed. The label is updated only if, for example I press a button which automatically refreshes the page.

Here is the code:

<asp:TextBox ID="username" runat="server" OnTextChanged="checkUsername" Width="80%"></asp:TextBox>

<asp:Label ID="usernameCheck" runat="server" CssClass="checkL"></asp:Label>

And the aspx.cs

protected void checkUsername(object sender, EventArgs e)
{
    if (username.Text.Length < 3 || username.Text.Length > 15)
    {
        //---Label = X (in red)
        usernameCheck.Text = "\u2715";
    }
    else
    {
        if (myBl.checkUsername(Convert.ToString(username)))
        {
            //---Label = X (in red)
            usernameCheck.Text = "\u2715";
        }
        else
        {
            //---Label = V (in green)
            usernameCheck.Text = "\u2713";
        }
    }

}


Thanks for any help.

1 Answer 1

1

You need to add AutoPostBack="true" to your TextBox. This will cause it to post back and for that server side event to fire.

There are much better ways of doing what you are trying to accomplish though, most of which do not require a full page postback. I would try to make an AJAX call using the javascript change event, and using something like a callback method.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for the answer. How do I do the same with a TextMode="password", I think the AutoPostBack="true" clears the password TextBox
When using textmode="password" the textbox will not persist after postback. This is a built in security measure. What exactly are you trying to accomplish with your password box?
First to validate the password and Second to compare between password and re-password
I would do that on the client side. Avoids a postback and provides a better user experience. As for validating the password how are you needing to validate it?
Thanks for the fast responses. Basically I need to check the password length and maybe I will add some forbidden character and the re-password check.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.