3

Hey, Is it possible with easy options in ASP.NET to set the focus at end of text in a textbox-element ? Without JavaScript ?

Patrik
  • 1,099
  • 5
  • 17
  • 37
  • 2
    You'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 Answers4

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
2

you can use this in serve-side:

ScriptManager.RegisterStartupScript(this, this.GetType(), "tmp2", "var t2 =     document.getElementById('foo'); t2.focus();t2.value = t2.value;", true);
GDP
  • 7,863
  • 6
  • 42
  • 79
habiat
  • 1,033
  • 3
  • 15
  • 21
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"/>

Fiddle Example

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