Causing an automatic message after entering text in a TextBox without pressing the enter key or tab

I want my asp.net TextBox to fire an automatic postback and fire the side side event when entering text in a TextBox without pressing enter or tab, like WinForms. Is it possible? If possible, will this only happen after entering a certain number of characters, such as three or four? Thanks.

0
source share
4 answers

You cannot compare Windows-Applications and WebApplications. To send back to the server when the user enters char means that all HTML must be recreated from ASP.NET and sent back to the client. But here is an example:

Aspx:

<script type="text/javascript"> var MIN_TEXTLENGTH = 3; function checkPostback(ctrl) { if (ctrl != null && ctrl.value && ctrl.value.length >= MIN_TEXTLENGTH) { __doPostBack(ctrl.id, ''); } } </script> <asp:TextBox ID="TextBox1" OnKeyUp="checkPostback(this);" AutoPostBack="true" OnTextChanged="TextChanged" runat="server"></asp:TextBox> 

Codebehind:

 Protected Sub TextChanged(ByVal sender As Object, ByVal e As EventArgs) TextBox1.Attributes.Add("onfocus", "javascript:this.value=this.value;") TextBox1.Focus() End Sub 
+2
source

TextChange The TextBox event will become a blur event in JavaScript. This means that it can send a message back only when you leave the control (TextBox, which on the client side is an input or text area), and the value changes. So this is not possible without tricks.

However, about your architecture (how you want to send information to the server), I have to say that it will not work buddy. Post backs are really expensive and you should avoid them as much as possible. My suggestion is to use ajax in conjunction with the keydown event in the input field.

0
source

You can do this using Ajax and the onkeypress event plus some kind of counter.

So, you connect the function to the onKeyPress event of your text field, which increases the counter every time it starts, when this number reaches 3 or 4, you set the ajax function, which sends the form to the server in the background and then reset the counter again.

This will cause the form to be published in the background for every third or fourth keystroke that occurs while using a text field, which is each keystroke - space, input, backspace, etc.

0
source

This sounds like a bad idea, but ...

Will you have only a java script that is read when a key is pressed and then triggers a message when a certain char limit has been reached?

A key press event with Ajax or just submit should work.

- It seems that another poster beat me before the strike.

0
source

All Articles