views:

528

answers:

3

I have an asp.net TextBox in which I want to check if the text entered into the TextBox is > 0. It works once I tab out or click out of the TextBox, but if I keep focus on the TextBox, it won't fire the Text Changed Event, so I have the following scenario, I want to enable something if and only if the TextBox.Text.Length = 0. Now, if I put my caret in the TextBox and delete all the characters and then leave the caret in the TextBox so it still has focus and take my mouse and click a button, it will not do what it was supposed to do because it never fired the Text Changed Event. How would something like this be handled?

+1  A: 

friend, keyup, keydown and keypress are your friends

Tzury Bar Yochay
Are these events for the TextBox?
Xaisoft
Yes, in javascript, you can bind on these. It is a little bit difficult in ASP.Net because you have to set the attributes in the code behind, but it is doable.
John Gietzen
+1  A: 

The best idea is to write some client-side javascript to do what you want. The TextChanged event handler requires a postback to the server, and posting back to the server before a text box loses focus is impossible. Unless that is what you intend, I would suggest the former.

jaywon
A: 

you can also use the setInterval javascript method to check for a change in the value of the textbox on a timed basis. just remember, you need to use the form name followed by control name and value to reference the control.

setInterval(MethodName, 100);

function MethodName()
{
   if(formname.controlid.value.length > 0)
   {
     //do something here
   {
}
Motten