Deep
Deep

Reputation: 81

Execute Text Box_TextChanged Event in Asp.Net using C#?

I am trying to implement validations on textbox like if null then display a message to fill the empty field and to check the length of the text entered. I have written the code for it inside TextBox_TextChanged event but it's not working. The event is not getting fired and the user can is able to signup without a username, that is my problem at the moment. Do I have to trigger the event manually? Here is a glimpse of what I am doing:

protected void FirstN_TextBox_TextChanged(object sender, EventArgs e)
    {
        String firstNameEntered = FirstN_TextBox.Text;
        if (firstNameEntered != null)
        {
            if (firstNameEntered.Length <= 20)
            {
                MessageBox.Show("Inside text box");
            }
            else
            {

            }

        }
        else
        {
            FirstN_TextBox.Focus();
            MessageBox.Show("Please fill the marked field");
        }

Upvotes: 1

Views: 484

Answers (1)

Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

change it like this:

From:

<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px"   OnTextChanged="FirstN_TextBox_TextChanged"></asp:TextBox>

To:

 <asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px"   OnTextChanged="FirstN_TextBox_TextChanged" AutoPostBack ="true"></asp:TextBox>

Upvotes: 2

Related Questions