Link to home
Start Free TrialLog in
Avatar of schwientekd
schwientekdFlag for United States of America

asked on

ASP.NET gridview select textbox on focus

I have a gridview on an asp.net webpage which has textboxes as template fields throughout each column.  When the gridview loads most of the textboxes are prepopulated with text.  I would like to be able to click on the textbox and have the text be selected (highlighted) so that when a change needs to be made the visitor does not need to select the text by dragging the mouse or double clicking on it.

I have tried using javascript to accomplish this by placing the following code into the page for a textbox I call tbTest which is OUTSIDE of the gridview (just for testing purposes).

'This is inserted into the source code of the aspx page
 <script type="text/javascript" language="javascript">
          function selectText() {
              document.getElementById("<%=tbTest.ClientID %>").select();
          }
    </script>

'This line is in the page load event of the code behind
tbTest.Attributes.Add("onfocus", "selectText();")

This works on the tbTest textbox but I do not know how to make this work for a textbox in the gridview.  I can access the textboxes by using the following line in the RowDataBound event.

Dim testString As String = TryCast(e.Row.FindControl("tbGridCol1"), TextBox).Text()

Can I add something to the RowDataBound event to accomplish this or is there another method anyone knows of?
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of schwientekd

ASKER

Bingo!  That worked perfectly.  I was able to remove this script from the source page code and just use the line in the RowDataBound event.  I also removed the line I was using in the page load event.

 <script type="text/javascript" language="javascript">
          function selectText() {
              document.getElementById("<%=tbTest.ClientID %>").select();
          }
    </script>