您的位置:首页 > Web前端 > JavaScript

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

2009-07-30 14:03 357 查看
首先在模板列中添加一个按钮用来选中行,页面有关代码如下:
<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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: