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,119
  • 5
  • 18
  • 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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
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
  • 8,109
  • 6
  • 45
  • 82
habiat
  • 1,245
  • 3
  • 16
  • 22
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
  • 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