Best way to focus and position cursor in textbox

Last post 07-03-2009 8:30 AM by DarrellNorton. 1 replies.

Sort Posts:

  • Best way to focus and position cursor in textbox

    07-03-2009, 4:14 AM
    • Member
      16 point Member
    • sparrez
    • Member since 12-04-2006, 1:51 PM
    • Posts 28

     Hi

    I'm developing a webpart to search for customers etc., but that is not really the important part.

    I'm using an UpdatePanel and need some sort of AutoComplete behavior, nothing advanced or anything. Just a list/table of results that is updated as the user changes the text in the input field. For this I'm using the onKeyUp event to catch keystrokes. But the postback behavior steals focus from the textbox so i use TextBox.Focus() method to set this (could not get at client script to work). This gives focus back to the TextBox but leaves the cursor at the beginning of the field. I then use another client script top change the position of the cursor to the end. This works great, but if you type to fast sometimes it has not moved the cursor when the next onKeyUp event takes place thus loosing some characters.

    This is the code that changes the position:

     function SetCursorToTextEnd(elemId) {
                var elem = document.getElementById(elemId);
                alert(window.event.which);
                if (elem != null) {
                    if (elem.createTextRange) {
                        var range = elem.createTextRange();
                        range.move('character', elem.value.length);
                        range.select();
                    }
                    else {
                        if (elem.selectionStart) {
                            elem.focus();
                            elem.setSelectionRange(elem.value.length, elem.value.length);
                        }
                        else
                            elem.focus();
                    }
                }
            }

    What would be the best way to implement a simple auto complete behavior for each keystroke in a textbox?

    Hope you can help. JavaScript isn't my strong side ;)

    --
    Christian

     

     

     

     

  • Re: Best way to focus and position cursor in textbox

    07-03-2009, 8:30 AM
    Answer
    • All-Star
      54,501 point All-Star
    • DarrellNorton
    • Member since 04-04-2003, 11:49 AM
    • VA, USA
    • Posts 6,571
    • Moderator
      TrustedFriends-MVPs

    Are you running into this problem with the ASP.NET AJAX AutoComplete extender?  It does not seem to have the problems you do.  Check it out to see if it meets your needs (very little JavaScript coding required):

    http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AutoComplete/AutoComplete.aspx

    You can also use a jQuery but that requires a bit more JavaScript and jQuery knowledge.  jQuery is supported as part of VS2008 and greater.

    Choose whichever one works for you.

    Darrell Norton, MVP
    Darrell Norton's Blog


    Please mark this post as answered if it helped you!
Page 1 of 1 (2 items)