Pages

Saturday, October 30, 2010

Set Cursor Location from server side

Javascript function
function setCursorPositionToEnd(elementId)
{ var elementRef = document.getElementById(elementId);
var cursorPosition = document.getElementById(elementId).value.length;
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();
}
}
}
}

use This code at server side
string myScript = String.Format("setCursorPositionToEnd('txtSearch');"); ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Myscript", myScript, true);

No comments:

Post a Comment

Search This Blog