TextBox to autopostback "automatically" when text.length = x
-
Hi All, i have 4 textboxes. when the length of text in first textbox is equal to 4 then it should "automatically" change its focus to next textbox. i have set the autopostback to true but i need to click on screen to make a postback. i used onKeyPress, onKeyUp, onTextChanged but nothing is working for me... or is there any mistake in my code.. .aspx page : javascript code : function textLength() { var val = document.getElementById("TextBox2").value; if (val.length == 4) { document.getElementById("TextBox3").Focus(); } } in .aspx.cs page: TextBox2.Attributes.Add("onKeyUp", "javascript: textLength();"); is there any way to achieve this.. Thanks in Advance.
-
Hi All, i have 4 textboxes. when the length of text in first textbox is equal to 4 then it should "automatically" change its focus to next textbox. i have set the autopostback to true but i need to click on screen to make a postback. i used onKeyPress, onKeyUp, onTextChanged but nothing is working for me... or is there any mistake in my code.. .aspx page : javascript code : function textLength() { var val = document.getElementById("TextBox2").value; if (val.length == 4) { document.getElementById("TextBox3").Focus(); } } in .aspx.cs page: TextBox2.Attributes.Add("onKeyUp", "javascript: textLength();"); is there any way to achieve this.. Thanks in Advance.
What do you mean by 'not working' ? I'd write a method that takes the id of the textbox you want to move to, the textbox you're in, and the length you're looking for, so you can reuse it. onkeyup="myfunc(this)" would pass the control instance, then you only need to look up the other one. Make sure that the client side id is 'TextBox2', it probably isn't. You use the ClientID property of the server control to get the client side Id. Never use a control you got with getElementById without checking first if it's null. You can also install firebug for firefox and step through your script to make sure it's called, and see how it's working. Or use alerts to tell you when they get called, if you want something less functional but easier to set up.
Christian Graus Driven to the arms of OSX by Vista.
-
What do you mean by 'not working' ? I'd write a method that takes the id of the textbox you want to move to, the textbox you're in, and the length you're looking for, so you can reuse it. onkeyup="myfunc(this)" would pass the control instance, then you only need to look up the other one. Make sure that the client side id is 'TextBox2', it probably isn't. You use the ClientID property of the server control to get the client side Id. Never use a control you got with getElementById without checking first if it's null. You can also install firebug for firefox and step through your script to make sure it's called, and see how it's working. Or use alerts to tell you when they get called, if you want something less functional but easier to set up.
Christian Graus Driven to the arms of OSX by Vista.
Hi, thanks for replying... my javascript is not executing . i verified my code many times. iam unable to figure it out (why javascript is not executing). this is my entire code: protected void page_load(object sender, EventArgs e) { if(Page.IsPostBack) { TextBox2.Attributes.Add("onKeyUp", "javascript: textLength();"); } } ]]> Untitled Page function textLength() { alert("its called"); var val = document.getElementById("TextBox1").value; if (val.length == 4) { document.getElementById("TextBox2").Focus(); } }
onKeyUp="textLength(this)" >
Is everything is correct ?
-
Hi, thanks for replying... my javascript is not executing . i verified my code many times. iam unable to figure it out (why javascript is not executing). this is my entire code: protected void page_load(object sender, EventArgs e) { if(Page.IsPostBack) { TextBox2.Attributes.Add("onKeyUp", "javascript: textLength();"); } } ]]> Untitled Page function textLength() { alert("its called"); var val = document.getElementById("TextBox1").value; if (val.length == 4) { document.getElementById("TextBox2").Focus(); } }
onKeyUp="textLength(this)" >
Is everything is correct ?
I think onkeyup should be all lowercase.
Christian Graus Driven to the arms of OSX by Vista.
-
I think onkeyup should be all lowercase.
Christian Graus Driven to the arms of OSX by Vista.
still its not working... any other suggestions.... thanks
-
still its not working... any other suggestions.... thanks
why should'nt you tried it in c# ontextchanged event and still if you want your javascrit running are you sure you checked advance browser settings to enable javascript (are you disabled them???? :-O ) or maybe this onkeyup="texlenght()" need a ";" or onkeyup="textlenght();" hope this should help.
-
still its not working... any other suggestions.... thanks
Make sure onkeyup is a javascript event, and look at the HTML that's being generated to see if it looks the way you'd expect.
Christian Graus Driven to the arms of OSX by Vista.