Alin Popa
Alin Popa

Reputation: 211

text changed event not fired

I need to add 1 event for textbox into my webpage (created in ASP.NET with C#) and I was declared in the Page_Load function and into asp syntax:

 protected void Page_Load(object sender, EventArgs e)
    {
        textbox1.TextChanged += new EventHandler(textbox1_TextChanged);
    }

public void textbox1_TextChanged(object sender, EventArgs e)
{
    if (textbox1.Text == "ABCD")
    {
        Image1.Visible = true;
        textbox1.Enabled = false;
    }
}

and into asp page i used this statement:

<asp:TextBox Width="200" ID="textbox1" runat="server"></asp:TextBox>

I did debug and found that not execute textbox1_TextChanged function

Why ?

Upvotes: 1

Views: 16950

Answers (3)

BoldAsLove
BoldAsLove

Reputation: 689

I think its also worth noting that the TextChanged event does not fire if the text value did not actually change, i.e. you set the text but you set it to the same value it had previously.

Upvotes: 0

Giuliano
Giuliano

Reputation: 192

Well, better late than never: you declared a method, strictly speaking, a handler for the event. But you didn't bind the event to the handler, like this:

<asp:TextBox Width="200" ID="textbox1" OnTextChanged="textbox1_TextChanged" runat="server"></asp:TextBox>

What you missed: OnTextChanged="textbox1_TextChanged"

In other words, your method would never be called because you never told the control that method was a handler for the event.

Upvotes: 0

user57508
user57508

Reputation:

you need to set AutoPostBack to true.
see msdn for this:

To have the TextChanged event cause an immediate posting, set the TextBox control's AutoPostBack property to true.

Upvotes: 11

Related Questions