Hey, Is it possible with easy options in ASP.NET to set the focus at end of text in a textbox-element ? Without JavaScript ?
Asked
Active
Viewed 1.1k times
3
-
2You'll need JavaScript to do this. – ceejayoz Nov 04 '10 at 12:54
-
this link has correct solution [Set focus to end of text in textbox after postback][1] [1]: http://stackoverflow.com/questions/4032888/set-focus-to-end-of-text-in-textbox-after-postback – PSR Mar 25 '13 at 09:40
4 Answers
5
ASP.NET textboxes render as standard HTML inputs (with type="text") and the only way to achieve what you are asking is through javascript:
var textbox = document.getElementById('foo');
textbox.focus();
textbox.value = textbox.value;
where foo
is the id of the generated input:
<input type="text" id="foo" name="foo" value="some text" />

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
1
Solution using jquery
$('#loginBtn').on('click',function(){
var val = $('#txtLoginName').val();
$('#txtLoginName').val('');
$('#txtLoginName').val(val);
$('#txtLoginName').focus();
});
html code
<input type="text" id="txtLoginName" value="test"/>

Opal
- 81,889
- 28
- 189
- 210

RENJITH VS
- 99
- 13
0
This even works for update panel, i believe all other situations too
ScriptManager.RegisterStartupScript(this, this.GetType(), "tmp2", "$('#ContentPlaceHolder1_TxtSupplier').focus();var value = $('#ContentPlaceHolder1_TxtSupplier').val();$('#ContentPlaceHolder1_TxtSupplier').val('');$('#ContentPlaceHolder1_TxtSupplier').val(value);", true);

Arun Prasad E S
- 9,489
- 8
- 74
- 87