如何使用javascript在选中网格视图复选框时创建表行

How to create a table row on selecting a gridview checkbox using javascript

本文关键字:复选框 创建 视图 网格 javascript 何使用 中网      更新时间:2023-09-26

我使用的是一个带有复选框的网格视图,在选中网格视图中的任何复选框时,我需要用它创建一个表行。我需要帮助使用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>

在复选框检查状态下调用的基本脚本(仅启动)

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

我没有在网格视图上尝试过,但您可以通过使用JS中元素的parentNode属性来选择'tr'元素。"tr"可能是您选择的行。

例如。

点击复选框,使用以下代码,

onclick="javascript:selectCustomers(this);"

然后在功能中,

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

检查您的html元素以了解复选框中有多少个父级。使用Mozilla或Chrome工具检查元素。