2012-07-11 128 views
0

我用复选框的gridview,我需要创建一个表格列出来检查gridview中的任何复选框。我需要帮助才能使用javascript获取gridview的选定行。如何创建表格行选择gridview复选框使用javascript

GridView的源代码

<asp:GridView ID="GrdCustomer" runat="server" BorderColor="#999999" CellPadding="3" 
     ForeColor="Black" GridLines="Vertical" Width="640px" AllowPaging="True" AutoGenerateColumns="False" 
     OnRowDataBound="GrdCustomer_RowDataBound"> 
     <Columns> 
      <asp:TemplateField HeaderText="Select" ItemStyle-Width="50px"> 
       <ItemTemplate> 
        <input id="selector" onclick="javascript:bindToList(this);selectCustomers();" runat="server" type="checkbox" /> 
       </ItemTemplate> 
       <HeaderTemplate> 
        <input id="selector" onclick="javascript:SelectDeselectAllCheckboxes(this);selectCustomers(); " runat="server" 
         type="checkbox" /> 
       </HeaderTemplate> 
       <ItemStyle Width="50px" HorizontalAlign="Center"></ItemStyle> 
      </asp:TemplateField> 
      <asp:BoundField DataField="Salutation" HeaderText="Salutation"> 
       <ItemStyle HorizontalAlign="Center" /> 
      </asp:BoundField> 
      <asp:BoundField DataField="Name" HeaderText="Client Name"> 
       <ItemStyle HorizontalAlign="Center" /> 
      </asp:BoundField> 
      <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Email"> 
       <ItemStyle HorizontalAlign="Center" /> 
      </asp:BoundField> 
      <asp:BoundField DataField="Title" HeaderText="Title"> 
       <ItemStyle HorizontalAlign="Center" /> 
      </asp:BoundField> 
      <asp:BoundField DataField="Id" HeaderText="Id" /> 
     </Columns> 
     <FooterStyle BackColor="#CCCCCC" /> 
     <PagerStyle BackColor="#999999" ForeColor="Black" /> 
     <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
     <AlternatingRowStyle BackColor="#CCCCCC" /> 
    </asp:GridView> 

Basic脚本调用的复选框检查状态(只启动)

function selectCustomers() { 
     alert("Hey I'm over here!!!"); 
    } 

回答

1

我没有试过在一个gridview,但你可以选择 'TR'元素通过使用JS中元素的parentNode属性。 'tr'可能是您选择的行。

例如。

在复选框的点击使用下面的代码,

onclick="javascript:selectCustomers(this);" 
在功能

然后,

function selectCustomers(chkbox) { 
     var desiredparentelement = chkbox.parentNode.parentNode; // Use parent property to get tr 
    } 

检查你的HTML元素知道复选框多少父母。使用Mozilla或Chrome工具检查元素。

+0

谢谢你......这是'parentElement' – NewBie 2012-07-11 07:13:05

相关问题