Skip to content Skip to sidebar Skip to footer

How To Get Column Value For A Given Row Within GridView Using Javascript In Asp.net?

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

Solution 1:

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;

Solution 2:

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[]


Solution 3:

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".


Post a Comment for "How To Get Column Value For A Given Row Within GridView Using Javascript In Asp.net?"