12 February 2009

Set cursor position Last ( TextBox )

function setCursorPosition(elementRef, cursorPosition)
{
if ( typeof elementRef == 'string' )
elementRef = document.getElementById(elementRef);

if ( elementRef != null )
{
if ( elementRef.createTextRange )
{
var textRange = elementRef.createTextRange();
textRange.move('character', cursorPosition);
textRange.select();
}
else
{
if ( elementRef.selectionStart )
{
elementRef.focus();
elementRef.setSelectionRange(cursorPosition, cursorPosition);
}
else
{
elementRef.focus();
}
}
}
}

--------------------

ScriptManager.RegisterStartupScript(this, this.GetType(), "k", "setCursorPosition('" + TextBox2.ClientID + "'," + TextBox2.Text.Length + ");", true);

No comments: