Textbox cursor position

After googling a lot
to set the cursor at the end of the textbox
I got some code snippets like

//javascript functions to move the cusor position at the end of textbox

    function SetEnd(TB) {

      
        if (TB.createTextRange) {
            var FieldRange = TB.createTextRange();
            FieldRange.moveStart('character', TB.value.length);
             FieldRange.collapse();
               FieldRange.select();
        } 
    } 



    function SetCursorToTextEnd(textControlID) {
        alert(textControlID);
        var text = document.getElementById(textControlID);
        alert(text.value.length);
        if (text != null && text.value.length > 0) {
            if (document.selection) {
                var range = document.selection.createRange();
                range.moveStart('character', text.value.length);
                range.collapse();
                range.select();
            }
            else if (window.getSelection) {

                var range = window.getSelection().getRangeAt(0);
                if (range.startContainer.nodeName == "#text") {

                    range.setStart(range.startContainer, text.value.length);
                    range.setEnd(range.startContainer, text.value.length);
                }
                else {
                    window.getSelection().removeAllRanges();
                    var div = document.createRange();
                    if (text.childNodes.length > 0 && text.childNodes[0].nodeName == "#text") {
                        div.setStart(text.childNodes[0], text.value.length);
                        div.setEnd(text.childNodes[0], text.value.length);
                        window.getSelection().addRange(div);
                    }
                }


            }
        }
    }  

but those are not working in my case
My scenario
After selecting text from Gridview row
I have to display selected text in textbox , appended by previously selected text in grid
text should be inserted at cursor position that i did using

<script LANGUAGE=“Javascript”>
var globalCursorPos; // global variabe to keep track of where the cursor was

    //sets the global variable to keep track of the cursor position
    function setCursorPos() {
        globalCursorPos = getCursorPos(form1.txtExp);
    }

    //This function returns the index of the cursor location in
    //the value of the input text element
    //It is important to make sure that the sWeirdString variable contains
    //a set of characters that will not be encountered normally in your
    //text
    function getCursorPos(textElement) {
        //save off the current value to restore it later,
        var sOldText = textElement.value;

        //create a range object and save off it's text
        var objRange = document.selection.createRange();
        var sOldRange = objRange.text;

        //set this string to a small string that will not normally be encountered
        var sWeirdString = '#%~';

        //insert the weirdstring where the cursor is at
        objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

        //save off the new string with the weirdstring in it
        var sNewText = textElement.value;

        //set the actual text value back to how it was
        objRange.text = sOldRange;

        //look through the new string we saved off and find the location of
        //the weirdstring that was inserted and return that value
        for (i = 0; i &lt;= sNewText.length; i++) {
            var sTemp = sNewText.substring(i, i + sWeirdString.length);
            if (sTemp == sWeirdString) {
                var cursorPos = (i - sOldRange.length);
                return cursorPos;
            }
            
           
           
        }
    }

    //this function inserts the input string into the textarea
    //where the cursor was at
    function insertString(stringToInsert) {
       
        var firstPart = form1.txtExp.value.substring(0, globalCursorPos);
        var secondPart = form1.txtExp.value.substring(globalCursorPos, form1.txtExp.value.length);
        form1.txtExp.value = firstPart + stringToInsert + secondPart;

    }




    }

</SCRIPT>

//javascript functions to insert the text at cursor location

but when i select consecutively on gridview rowws , previous text gets repeat

gridview rows

1
2
3

if i select 1,2 consequtively expected text = 12
but it appears as 212 in textbox

this happens when i use the above said javascript functions to move the cusor position at the end of textbox

The best resource for learning about how to script text selection techniques in a cross-browser manner, is from the quirksmode page Introduction to Range