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.


WebForm1.aspx.cs


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;

          gridviewSearchResult.DataBind();

        }

      }

 

   

    }

}

 

 

WebForm1.aspx



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

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

   

    <script type="text/javascript">

      function Test() {

        //alert('Hi');

        // debugger;

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

        var gvRowCount = gv.rows.length;

        var rwIndex = 1;

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

          alert(gv.rows[rwIndex].cells[0].childNodes[1].innerHTML);

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

            alert("is 50");

          } else

          { alert("is not 50"); }

        }

        return false;

      }

    </script>

</head>

<body>

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

    <div>

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

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

        <Columns>

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

            <ItemTemplate>

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

            </ItemTemplate>

          </asp:TemplateField>

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

            <ItemTemplate>

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

            </ItemTemplate>

          </asp:TemplateField>

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

            <ItemTemplate>

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

            </ItemTemplate>

          </asp:TemplateField>

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

            <ItemTemplate>

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

            </ItemTemplate>

          </asp:TemplateField>

        </Columns>

      </asp:GridView>

    </div>

    </form>

</body>

</html>

 



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,

Vickey

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 textbox.id

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