Set Focus at end of text in textbox
Hey, Is it possible with easy options in ASP.NET to se开发者_如何学JAVAt the focus at end of text in a textbox-element ? Without JavaScript ?
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" />
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);
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
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);
精彩评论