Setting cursor position on textbox after postback
<p>I am able to focus on the textbox after postback using this code:</p>

<pre><code>ScriptManager.RegisterStartupScript(textBox, textBox.GetType(), "selectAndFocus", "$get('" + textBox.ClientID + "').focus();", true);
</code></pre>

<p>But this sets the cursor position to the beginning of the textbox, not after the last typed character. I try to solve this by using this code:</p>

<pre><code>textBox.Attributes.Add("onfocus", "$get('" + textBox.ClientID + "').value = $get('" + textBox.ClientID + "').value;");
</code></pre>

<p>But that doesn't work. Same result as before.
How can I solve this? </p>

<p>I have read a ton of links, <a href="http://parentnode.org/javascript/working-with-the-cursor-position/" rel="nofollow">this</a> seeming like the best solution, but I haven't been able to get it to work. </p>

<p>UPDATE: Forgot to mention that the textbox resides inside an updatepanel.</p>

<p>UPDATE2, attempted solution:</p>

<pre><code>string setCaretTo = @"function setCaretTo(obj, pos) { 
    if(obj.createTextRange) { 
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = obj.createTextRange(); 
        range.move('character', pos); 
        range.select(); 
    } else if(obj.selectionStart) { 
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */ 
        obj.focus(); 
        obj.setSelectionRange(pos, pos); 
    } 
}";
            ScriptManager.RegisterClientScriptBlock(//anotherunrelated script);
            ScriptManager.RegisterClientScriptBlock(textBox, textBox.GetType(), "MyScript", setCaretTo, true);
            ScriptManager.RegisterStartupScript(textBox, textBox.GetType(), "MyStartupScript", "window.onload = function() {obj = window.document.getElementById('"+textBox.ClientID+"');setCaretTo(obj, obj.getAttribute('value').length);}", true);`
</code></pre>
