Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
980
get cursor position on click
posted

Hello,

Is it possible to get the cursor position (probably by selection) on click on webtexteditor? If it's possible on the client, how would you recommend to persist it to the server?

  • 980
    Verified Answer
    posted

    Hello, 

    Probably it will be helpful to someone.

    First, add click event. Or you can also use mouseUp client event.

    For example, like this on the serve in Page_load:

    this.WebTextEditor1.Attributes.Add("onclick", "func()");

    The same is valid for asp net textbox.

    Getting cursor be done like this on the client side:

    function getposition() {

    var txt1 = $find("<%= webtexteditor1="" clientid="">");

    //returns index of first selected character
    var start = txt1.getSelection(true);

    //returns index of last selected character
    var stop = txt1.getSelection(false);
    if (stop > start)
        return "SELECTION";
    else
         return start;

    }

    It can be done aslo for usual asp.net textbox like this:

    function getposition() {

    //code for usual asp net textbox
    var txt1 = document.getElementById("TextBox1");
    var currentRange = document.selection.createRange();
    var selectedText = currentRange.text;
    if (selectedText.length != 0)
       return "SELECTION";
    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();

    return len;

    }

    Then, you can either manually implement AJAX, submit or anything you prefer. Also, you can use some Infragistics control for sending data to the server.

    For example - webdropdown. Place it in some not visible area of your page. Don't set it's property Visible to false, or you'll not be able to get webdropdown on the client. You can pass then cursor position and any other data as string, like this

    function func() {
    var position = getposition();
    if (position == "SELECTION")
          return;

    var wdd= $find("<%= webdropdown1="" clientid="">");
    wdd.loadItems(position, true);

    }

    And then get your position on the server in ItemsRequested event of webdropdwon:

    e.Value.

    For example:

    protected void wdd_ItemsRequested(object sender, DropDownItemsRequestedEventArgs e)
    {
        int positionOfCusrsor = Convert.ToInt32(e.Value);

    }