Textbox Selection

4 posts, 0 answers
  1. Anzar
    Anzar avatar
    62 posts
    Member since:
    Sep 2012

    Posted 18 Sep 2012 Link to this post

    Hi,
       I have a telerik grid with template columns. One of the column has textbox as edit item template. And write onkeypress event for this textbox using javascript. On this event How i will identify the text inside the textbox is selected or not.

    Thanks & Regards
    Anzar.M

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Sep 2012 Link to this post

    Hi Anzar,

    Here is the sample code that I tried to check if the text is selected in TextBox.
    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
       if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
         GridEditableItem item = (GridEditableItem)e.Item;
         TextBox TextBox1 = (TextBox)item.FindControl("TextBox1");
         TextBox1.Attributes.Add("OnKeyPress", "GetKeyPress(this)");
       }
    }
    JS:
    function GetKeyPress(e)
     {
      if (document.selection)
      {
        alert("selected");
      }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Anzar
    Anzar avatar
    62 posts
    Member since:
    Sep 2012

    Posted 19 Sep 2012 Link to this post

    Dear Shinu,
    That display undefined while selected or not.Pl's provide more better solution.

    Thanks & Regards
    Anzar.M
     
  5. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 21 Sep 2012 Link to this post

    Hi Anzar,

    Could you please try the following approach?
      mark-up:
    <asp:TextBox ... onkeypress="keyPressed(this)"></asp:TextBox>
      JavaScript:
    function keyPressed(textBox) {
        var selectedLength = textBox.selectionEnd - textBox.selectionStart;
        var totalLength = textBox.value.length;
        if (selectedLength == totalLength && totalLength > 0) {
            alert("entire text is selected");
        }
    }

    That should do the trick. Please give it a try and let me know about the result.

    All the best,
    Eyup
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top