This is a migrated thread and some comments may be shown as answers.

Textbox Selection

3 Answers 69 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anzar
Top achievements
Rank 2
Anzar asked on 19 Sep 2012, 04:33 AM
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

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Sep 2012, 05:40 AM
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.
0
Anzar
Top achievements
Rank 2
answered on 19 Sep 2012, 08:52 AM
Dear Shinu,
That display undefined while selected or not.Pl's provide more better solution.

Thanks & Regards
Anzar.M
 
0
Eyup
Telerik team
answered on 21 Sep 2012, 12:34 PM
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.
Tags
Grid
Asked by
Anzar
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Anzar
Top achievements
Rank 2
Eyup
Telerik team
Share this question
or