Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding javascript to the OnBlur property of an ASP.NET text box control

Is there a way to specify some JavaScript to execute on the OnBlur event of an ASP.NET text box? It seems to me like if I add any event handlers to the TextBox object they will just cause postbacks to the server isntead of doing what I want. Basically, I just want to be able to have the textbox be rendered in this HTML:

<INPUT type="text" onblur="alert('1234')" />

Thanks!

like image 606
skb Avatar asked Dec 21 '08 19:12

skb


People also ask

What is Onblur event of textbox in asp net?

The onblur event occurs when an object loses focus.

What is Onblur attribute in Javascript?

The onblur attribute fires the moment that the element loses focus. Onblur is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur attribute is the opposite of the onfocus attribute.

Can we apply Onblur on Div?

The blur event fires when focus is lost. By default, a div element cannot have the focus in the first place so it can't be lost.

Does tab trigger Onblur?

In browsers, pressing the tab key when a control has focus will move it to the next element in the tab order, causing the current element to lose focus and dispatch a blur event.


2 Answers

Could also go for:

<asp:TextBox runat="server" onblur="Javascript:alert('1234');" />

if you dont feel like setting it up in the codebehind.

Im guessing the reason why you end up with postbacks, must be because you have set AutoPostBack on the textbox to true. That makes the textbox postback when the client-side onchange event is triggered. Switch it to false, and it will act as a normal input-element.

like image 69
Tom Jelen Avatar answered Sep 23 '22 01:09

Tom Jelen


In your codebehind, add this:

myTextBox.Attributes.Add("onblur","alert('1234');");
like image 30
FlySwat Avatar answered Sep 24 '22 01:09

FlySwat