vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Friday, June 1, 2012

How to get the selected row value in gridview using javascript?


In this article about to pick the selected row data from gridview using javascript.

Inside gridview i added one html checkbox, when the user select any  checkbox ,on click event we call the javascript and show the selected column value using alert, it’s very simple process. on click event we pass the row item index, by using row index we can easily get selected  any row column values.
By getting column values using rowindex can achieve only if we use Bound field in gridview..


Code:
   <script type="text/javascript">
        function chng(t, obj) {
            if (obj.checked == true) {
                var CellValue, cell, tj;
                var table = document.getElementById('<%=GridView1.ClientID %>');
                cell = document.getElementById('<%=GridView1.ClientID %>').rows[parseInt(t) + 1].cells[1];
                tj = cell.innerHTML;
                alert(tj);
            }
        }

    </script>

<asp:GridView ID="GridView1" AutoGenerateColumns="False" AutoGenerateSelectButton="false"
        AllowPaging="True" SelectedIndex="1" AllowSorting="true"
runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <input type="checkbox" value="getIndex" onclick="chng(<%# Container.DataItemIndex %>,this);" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" InsertVisible="False" ReadOnly="True"
                SortExpression="Name" />
            <asp:BoundField DataField="roll" HeaderText="Address" SortExpression="Address" />
        </Columns>
        <SelectedRowStyle BackColor="LightCyan" ForeColor="DarkBlue" Font-Bold="true" />
    </asp:GridView>

Above code works perfectly, In an alert window its show the selected value from the grid view..
I hope it’s helpful…keep reading

0 comments:

Post a Comment