How to get cursor position in asp:textbox?

Last post 08-15-2008 4:41 AM by Lance Zhang - MSFT. 5 replies.

Sort Posts:

  • How to get cursor position in asp:textbox?

    08-12-2008, 4:51 AM

    Hi to all,

     First of all sorry for my poor english.

     I have a requirement to insert a text strings in a asp:textbox at the position where the cursor is placed in textbox.

     Actually I have required to insert the item selected from listbox into textbox at button click event at the place where the cursor is placed in textbox. I think it can be done at serverside if i get the cursor position of textbox but i cannot get the cursor position of text box at server side.

     If their  is any possible solution then please help me.

     

    Regards,

    Sandeep Soni

     

    Sandeep
  • Re: How to get cursor position in asp:textbox?

    08-12-2008, 5:10 AM
    • Contributor
      5,079 point Contributor
    • NHOQUE
    • Member since 04-02-2008, 9:00 AM
    • Kumamoto, Japan
    • Posts 849

    There is no serverside option to detect cursor position, so far I know. Javscript can detect your cursor position in clientside. But what you want to do is not clear to me.

    You can focus your textbox by using

    TextBox1.Focus;

    Thanks.

    HOQUE MD.NAZMUL
    [document.getReaders]
  • Re: How to get cursor position in asp:textbox?

    08-12-2008, 7:21 AM

    Thanks for your reply.

     Can you please get me know how i can get the cursor postion of textbox in javascript? I need to know the cursor position in textbox at click event of button.

     

    Regards,

    Sandeep Soni

    Sandeep
  • Re: How to get cursor position in asp:textbox?

    08-12-2008, 7:23 AM

    Thanks for your reply.

     Can you please get me know how i can get the cursor postion of textbox in javascript? I need to know the cursor position in textbox at click event of button.

     

    Regards,

    Sandeep Soni

    Sandeep
  • Re: How to get cursor position in asp:textbox?

    08-12-2008, 8:09 AM
    Answer
    • All-Star
      76,585 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 14,267

    Try this:

    <script type="text/javascript">
    <!--
    function insertAtCursor(elementRef, valueToInsert)
    {
     if ( document.selection )
     {
      // Internet Explorer...

      elementRef.focus();
      var selectionRange = document.selection.createRange();
      selectionRange.text = valueToInsert;
     }
     else if ( (elementRef.selectionStart) || (elementRef.selectionStart == '0') )
     {
      // Mozilla/Netscape...

      var startPos = elementRef.selectionStart;
      var endPos = elementRef.selectionEnd;
      elementRef.value = elementRef.value.substring(0, startPos) +
       valueToInsert + elementRef.value.substring(endPos, elementRef.value.length);
     }
     else
     {
      elementRef.value += valueToInsert;
     }
    }
    // -->
    </script>

    NC...

  • Re: How to get cursor position in asp:textbox?

    08-15-2008, 4:41 AM
    Answer
    Hi sonsandeep@gmail.com
     
    If you would like to get the cursor position in TextBox, please check the following thread:
     
    Cursor position and select all text in javascript
    http://forums.asp.net/t/1295430.aspx
     
     

    Lance Zhang - MSFT:

    if you want get the current cursor position in the TextBox, please try the following code:
    (Code from http://topic.csdn.net/t/20020130/09/507998.html)
     
        function getposition()
        {
            var txt1=document.getElementById("TextBox1");
       
            var currentRange=document.selection.createRange();  
            var workRange=currentRange.duplicate();
            txt1.select();
            var allRange=document.selection.createRange();
            var len=0;  
       
            while(workRange.compareEndPoints("StartToStart",allRange)>0)  
            {  
              workRange.moveStart("character",-1);  
              len++;  
            }  
       
            currentRange.select();  
           
            window.alert(len);
    }
     


     
    I look forward to receiving your test results.
    Thanks.

     

    Lance Zhang
Page 1 of 1 (6 items)