繁体   English   中英

如何使用javascript选择GridView复选框来创建表格行

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

我正在使用带有复选框的gridview,并且在检查gridview中的任何复选框时需要从中创建一个表格行。 我需要帮助以使用javascript获取gridview的选定行。

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!!!");
    }

我没有在gridview上尝试过,但是可以通过使用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工具检查元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM