1

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 ?

Greg B
  • 14,597
  • 18
  • 87
  • 141
Alin Popa
  • 211
  • 2
  • 4
  • 14

3 Answers3

11

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.

  • 2
    But remember: "The TextBox Web server control does not raise an event each time the user enters a keystroke, only when the user leaves the control." – Codesleuth Feb 09 '10 at 11:37
  • which is caused by definition of `changed` event of the xhtml-tag :) –  Feb 09 '10 at 11:39
  • How then does one fire an event on each keystroke? – Pete Alvin Feb 22 '16 at 17:48
  • @PeteAlvin quick and dirty: attaching to `keyup` and triggering a `change` with eg jQuery. But honestly I see no use in this problem, as this would mean that the response-time has to cope with the strokes-interval (depending on your actual typing speed, something aeound 300 strokes per minute, which means 5 strokes per second, which means a response-time of 200ms). –  Feb 22 '16 at 22:46
  • in my case i have auto post back set to true but i dont undertand why the Text changed server side event is not firing when value is set to text box from a dataset,please suggest if i can manually fire that text changed event as that event holds the code to bind the other two drop downs in the page. – clarifier Jun 08 '17 at 14:35
  • @clarifier yep, you can use `__doPostBack` manually (https://stackoverflow.com/questions/3591634/how-to-use-dopostback), but this is imho an evil hack :) but, there should already be a `__doPostBack`-invocation in a corresponding html-event. can you verify whether a POST is made and the server-side eventHandler is not triggered (which would be caused by a viewstate, id, ... missmatch - do you build your controls in the correct lifecycle-step)? –  Oct 25 '17 at 09:32
0

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.

Giuliano
  • 172
  • 9
0

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.

BoldAsLove
  • 660
  • 4
  • 13
  • 1
    @Rob: thank you for the guidelines.. I will take a look into the suggested link and will pay more attention while reviewing the posts. once again thanks for the suggestions. – sujith karivelil Feb 23 '16 at 04:30