How to find text-box in GridView using Javascript

Total Post:119

 1993  View(s)
Rate this:

I am trying to get access and find text-box in GridView using javascript but i am getting an error: 'The name txt_UID' does not exist in the current context'. Everything worked fine when my text-box was outside of the GridView. Here is my text-box in the gridview and my gridview is called GridView1: 

 <asp:TemplateField ItemStyle-Width="150px" HeaderText="User Assigned">
     <asp:TextBox ID="txt_UID" runat="server" Text='<%# Eval("UID")%>'
        CssClass="AutoCompleteTextBox" Width="130px" BackColor="LightGoldenrodYellow"></asp:TextBox>
         <ItemStyle Width="150px" />

Here is my JavaScript: 

  <script type ="text/javascript">
        function setAutoComplete() {
            var textBoxes = document.getElementsByClassName("AutoCompleteTextBox");
            for (var i = 0; i < textBoxes.length; i++) {
<script type="text/javascript">
    function addAutoComplete(textBoxId) {
        $("#" + textBoxId).autocomplete({
            source: function (request, response) {
                    url: '<%=ResolveUrl("~/Service.asmx/GetUserNames") %>',
                    data: "{ 'prefix': '" + request.term + "'}",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('-')[0],
                                val: item.split('-')[1]
                    error: function (response) {
                    failure: function (response) {
            select: function (e, i) {
                $("#<%=hfUserId.ClientID %>").val(i.item.val);
            minLength: 1
<script type ="text/javascript">
     $(document).ready(function () { setAutoComplete(); });

  1. Post:110

    Re: How to find text-box in GridView using Javascript

    Your Gridview will be rendered as Table and your control will be contained in cell of table. You can give a try to following. 

    <script type=”text/javascript”>
    tblTable=document.getElementById(‘<<Client ID of the GridView>>’);
    Cell=tblTable.rows[i].cells[j];//i and j are locations of that cell.
    FirstControl = Cell.childNodes[0];

    replace <<Client ID of the GridView>> with id of your GridView

      Modified On Apr-07-2018 05:12:47 AM
  1. Post:104

    Re: How to find text-box in GridView using Javascript

    you can use name attribute and the grid id to find it: 

    <asp:TextBox ID="txt_UID" name="mytextbox" runat="server" Text='<%#Eval("UID")%>'
            Width="130px" BackColor="LightGoldenrodYellow"></asp:TextBox>
    the javascript part: $("#<%=MyGrid.ClientID %>[name=mytextbox]").autocomplete({});

      Modified On Apr-07-2018 05:12:27 AM