JavaScript获取GridView控件中选中行的行号和指定列的值

首先在模板列中添加一个按钮用来选中行,页面有关代码如下:

<asp:GridView ID="gdview" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
                                            Height="176px" Width="380px" OnRowDataBound="gdview_RowDataBound" OnSelectedIndexChanging="gdview_SelectedIndexChanging">
                                            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                                            <RowStyle BackColor="#E3EAEB" />
                                            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                                            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                                            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                                            <EditRowStyle BackColor="#7C6F57" />
                                            <AlternatingRowStyle BackColor="White" />
                                            <Columns>
                                                <asp:TemplateField>
                                                    <ItemTemplate>
<input id="btnLocation" type="button" value="定位到" onclick="Select()" style="height: 20px" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>

脚本代码如下:

//取得gridview中获取选中行指定列的值
      function Select()
      {
            //获取鼠标点击的元素
            var e=event.srcElement;
            //获取元素所在的行的行号(表头行号从0开始)。注意:parentElement只适用于IE浏览器,而parentNode则符合DOM标准。
            //var rowIndex=e.parentElement.parentElement.rowIndex ;
            var rowIndex=e.parentNode.parentNode.rowIndex ;
            //获取GridView控件
            var gdview=document.getElementById("<%=gdview.ClientID %>");
            //分别获取选定行指定列的值
            var value=gdview.rows(rowIndex).cells(列号).innerText;
      }

posted on 2011-09-26 21:20  不悔的青春  阅读(1277)  评论(0编辑  收藏  举报

导航