zoukankan      html  css  js  c++  java
  • 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;
          }

  • 相关阅读:
    vue 中使用阿里iconfont彩色图标
    团队作业九
    团队作业八
    团队作业七
    第二篇
    第三篇
    第一篇
    beta冲刺计划安排
    团队作业六
    团队作业五
  • 原文地址:https://www.cnblogs.com/zhangzt/p/2192285.html
Copyright © 2011-2022 走看看