3

I have a GridView, I have a row index in a javascript function. Now I want to get the value of a Column for that index in a GridView using javascript. Please tell me how can I do this?

1
  • Please show us the generated html in relation to you question. Commented Mar 19, 2014 at 12:00

3 Answers 3

5

Check this out...

 var tbl = document.getElementById('Gridview1');

 var tbl_row = tbl.rows[parseInt(RowIndex) + 1];

 var tbl_Cell = tbl_row.cells[no of the cell];

 var value= tbl_Cell.innerHTML.toString();

Here no of the cell indicates the column number.

If it is a template field,you can try this..

var value=document.getElementById('GridViewId_ColumnID_' + RowIndex).value;
Sign up to request clarification or add additional context in comments.

7 Comments

Its working fine. But I want to get the value at this variable value in your code. How to do this.
The variable 'value' has the value of the paricular cell..Is it a template feild?
yes template field. alert(value); is coming. <span id="GridViewId_ColumnID_0">Pending</span> I want to get the value at this id , in my case its Pending.
alert(value); is coming undefined :(
I have edited the answer..var value=document.getElementById('GridViewId_ColumnID_' + RowIndex).value;
|
1

A simple example.

 function myfunc   {
         var b=document.getElementById("GridView1");
         var c=document.getElementById("TextBox1");
         var d=document.getElementById("TextBox2");
         dd=dd+1;
         c.value=document.getElementById("GridView1").rows[dd].cells[2].innerHTML;
         d.value=document.getElementById("GridView1").rows[dd].cells[3].innerHTML;
    }

Get the id of the grid. Then you play around it by using rows[] and cells[]

Comments

0

Code behind page :

    protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "callFunctions('" + e.Row.RowIndex + "')");

           // e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
            e.Row.ToolTip = "Click to select this row.";
        }
    }

Javascript :

   <script type="text/javascript">
    function callFunctions(i) {
        //alert('Welcome');
        if (window.opener != null && !window.opener.closed) {
            var tbl = document.getElementById('GridView1');
            var tbl_row = tbl.rows[parseInt(i) + 1];
            var tbl_Cell = tbl_row.cells[0];
            var value = tbl_Cell.innerHTML.toString();
            var txtName = window.opener.document.getElementById("txtName");
            txtName.value = value;
        }
        window.close();
    }

</script>

If EnableEventValidation error occurring mean set its EnableEventValidation="false".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.