ASP.NET - Not getting values from textbox inside gridivew using javascript

Asked By vickey ap on 10-Apr-13 05:57 AM

Hi Experts,

I am here trying to achieve some thing using java script. I am posting the complete code below.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;


namespace testjs


    public partial class WebForm1 : System.Web.UI.Page


      static DataTable GetTable()



        // Here we create a DataTable with four columns.


        DataTable table = new DataTable();

        table.Columns.Add("Dosage", typeof(int));

        table.Columns.Add("Drug", typeof(string));

        table.Columns.Add("Patient", typeof(string));

        table.Columns.Add("Date", typeof(DateTime));


        // Here we add five DataRows.


        table.Rows.Add(25, "Indocin", "David", DateTime.Now);

        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);

        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);

        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);

        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

        return table;



      protected void Page_Load(object sender, EventArgs e)


        if (!IsPostBack)



          DataTable dt = new DataTable();

          dt = GetTable();

          gridviewSearchResult.DataSource = dt;











<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="testjs.WebForm1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head runat="server">



    <script type="text/javascript">

      function Test() {


        // debugger;

        var gv = document.getElementById("<%=gridviewSearchResult.ClientID %>");

        var gvRowCount = gv.rows.length;

        var rwIndex = 1;

        for (rwIndex; rwIndex <= gvRowCount - 1; rwIndex++) {


          if (gv.rows[rwIndex].cells[0].childNodes[1].innerHTML == "50") {

            alert("is 50");

          } else

          { alert("is not 50"); }


        return false;





    <form id="form1" runat="server">


      <asp:Button runat="server" ID="btntest" Text="Test" OnClientClick="javascript:Test(); return false;" />

      <asp:GridView ID="gridviewSearchResult" AutoGenerateColumns="false" runat="server" ClientIDMode="Static">


          <asp:TemplateField HeaderText="Dosage" SortExpression="Dosages">


              <asp:Label ID="txt1" runat="server" Text='<%# Eval("Dosage") %>'></asp:Label>



          <asp:TemplateField HeaderText="Drugs" SortExpression="Drugs">


              <asp:TextBox ID="txt2" runat="server" Text='<%# Eval("Drug") %>'></asp:TextBox>



          <asp:TemplateField HeaderText="Patient" SortExpression="Patient">


              <asp:TextBox ID="txt3" runat="server" Text='<%# Eval("Patient") %>'></asp:TextBox>



          <asp:TemplateField HeaderText="Date" SortExpression="Date">


              <asp:TextBox ID="txt4" runat="server" Text='<%# Eval("Date") %>'></asp:TextBox>










Here I have some issues,

When I click on the button , I am getting the values  of “Dosage” column in the gridview. But when I am trying to get the values of “Drugs” Its showing null. The Dosage Column is from Label and Drugs are of TextBox.

How to get value of the “Drugs” column here?

I want to check if the value of the Drugs column is equal to “Hydralazine” , the corresponding “Patients” textbox should set to disable/readonly?

I can only use javascript for that. Can any one modify the code and correct me.

Lots of thanks,


Robbe Morris replied to vickey ap on 10-Apr-13 08:59 AM
childNodes[1] is a zero based index.  Are you sure you are point at the right column?

That said, you might be able to make this easier by adding another column to your datatable to serve as a unique row id.  An int based key that could represent the row number.  Then, bind each control element in your Grid row's

ID='txtBoxColumn0_<%# Eval("MyRowIndex") %>'

and maybe

ID='txtBoxColumn1_<%# Eval("MyRowIndex") %>'

and so on.  This would enable you to easily use document.getElementByID to get any column value if you know the row index.  No need to iterate through a bunch crap in JavaScript