1

I'm trying to add a search functionality where the website sends a request to the server to gather information each time the user enters text (letter by letter). For this I'm in need of OnTextChanged event I assume? I tried using it but the event won't fire.

<asp:TextBox ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged"  runat="server"></asp:TextBox>

And my code behind:

protected void futu_search_TextChanged(object sender, EventArgs e)
{
       //I kept a break point here to see if it's fired
       //Send requests
}

What do you think is wrong? Do yall have a better solution than the 'OnTextChanged event' to get each key press and for a search box?

Jay
  • 4,873
  • 7
  • 72
  • 137

5 Answers5

3

Enable autopostback true ..

<asp:TextBox AutoPostback="true" ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged"  runat="server"></asp:TextBox>

If you want to fire the event when user press key than you have to use java script **onkeyup ** event

<asp:TextBox onkeyup ="return abc(event)" AutoPostback="true" ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged"  runat="server"></asp:TextBox>


<script>
function abc(evt) {

         ..put here your logic
        }
</script>
Dhaval
  • 2,341
  • 1
  • 13
  • 16
0

I think its better to use ajax and jquery for what you are trying to accomplish than to postback every key press.

Please check this LINK for more details and example.

Also, the OnTextChanged event fires when you loose focus and not while you type, which seems your requirement.

From an answer in CodeProject:

TextChanged: "Occurs when the content of the text box changes between posts to the server." AutoPostBack: "Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus."

Sample code :

Source : https://jqueryui.com/autocomplete/

<script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",          
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>

IMPORTANT : If you still plan to use keypress then read below quote,

The value of the input text box, during onKeyPress is always the value before the change.

Also it wont fire for "backspace" in chrome. Sample Fiddle

If you are still interested in OnKeyPress, I suggest you to have a look at this SO Question.

This is what you are trying to do, but the question is old and newer and better things have come up like HTML 5.

Note : Using on KeyUp Event would not trigger if you use Right click => Paste. To check here is a Fiddler link.

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
0

First of all, the OnTextChanged event will not trigger as its AutoPostBack property is false by default. So to make it happen please set its value to true.

And OnTextChanged event will only trigger when the textbox focus lost. So in this case you can use AJAX to get the value.

For that, you can use onkeyup to get the value in a javascript function at every characted enter.

NCA
  • 810
  • 1
  • 8
  • 19
  • Is there a OnKeyPress event? – Jay May 13 '15 at 05:49
  • No. But onkeypress event is there which is a javascript event. keypress happens before the change, keyup happens after. So in his case you can use onkeyup to get the value. – NCA May 13 '15 at 06:01
0

You can use the following code to set the focus:

ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(myTextBox);

To set focus and select text of a you have to use the following:

ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + myTextBox.ClientID + "').focus();$get('" + myTextBox.ClientID + "').select();", true);
Abhilash Thomas
  • 833
  • 2
  • 12
  • 23
-1

Include Autopost back tag in the control tag. AutoPostback="true"

<asp:TextBox AutoPostback="true" ID="txtcontrol"  OnTextChanged="txtcontrol_TextChanged"  runat="server"></asp:TextBox>
Rajamohan Rajendran
  • 369
  • 1
  • 3
  • 13