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.0k 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
- 994,864
- 265
- 3,241
- 2,902
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
- 76,740
- 25
- 177
- 200
RENJITH VS
- 99
- 12
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
- 8,451
- 8
- 71
- 81