选择GridView(asp.net)中特定列中的所有CheckBoxes

时间:2014-01-09 10:39:01

标签: asp.net select gridview checkbox

我有一个gridview,其中包含2个带复选框的coulmns。 我有一个标题列,其复选框名为Approve all,另一个名为Reject all。 当我单击其中一个复选框时,将检查该gridview中存在的所有子复选框,但我只想检查特定列复选框。请帮忙:

<script type="text/javascript" language="javascript">
    function checkAll(objRef) {
    var GridView = objRef.parentNode.parentNode.parentNode;
    var inputList = GridView.getElementsByTagName("input");

    for (var i = 0; i < inputList.length; i++) {
        var row = inputList[i].parentNode.parentNode;

            if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
                if (objRef.checked) {
                    row.style.backgroundColor = "#c3d9ff";
                    inputList[i].checked = true;
                }
                else {
                    if (row.rowIndex % 2 == 0) {
                        row.style.backgroundColor = "#c3d9ff";
                    }
                    else {
                        row.style.backgroundColor = "white";
                    }

                    inputList[i].checked = false;
                }
            }
        }
    }
</script>

<asp:GridView ID="gvDetails" runat="server" AlternatingRowStyle-CssClass="myGridAlternatingItemStyle" Width="98%" Height="500px" AllowPaging="false" ShowFooter="false" AutoGenerateColumns="False" >
    <FooterStyle CssClass="myGridTextItem" />
    <RowStyle CssClass="myGridTextItem"/>
    <SelectedRowStyle CssClass="myGridSelectedItemStyle"/>
    <%-- <HeaderStyle CssClass="myGridHeader" />--%>
    <AlternatingRowStyle CssClass="myGridAlternatingItemStyle" />
    <HeaderStyle CssClass="GVFixedHeader" />
    <Columns>
        <!-- I have few Data columns here -->
        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="checkAll" Text="Approve All" TextAlign="Left" runat="server" onclick="checkAll(this);" />
            </HeaderTemplate>
            <HeaderStyle CssClass="myGridHeader" ForeColor="White" HorizontalAlign="Center" Width="90px" />
            <ItemStyle HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:CheckBox ID="CheckApprove" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="RejAll" Text="Reject All " TextAlign="Left"  runat="server" onclick="checkAll(this);" />
            </HeaderTemplate>
            <HeaderStyle CssClass="myGridHeader" ForeColor="White" HorizontalAlign="Center" Width="80px" />
            <ItemStyle HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:CheckBox ID="CheckReject" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>                   
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

使用jquery来解决可能会减少时间的此类问题

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript">
    function checkAll(data) {
        $('[id$=checkAll]').click(function() {
            $("[id$='CheckApprove']").attr('checked', true);
        });     
        $('[id$=RejAll]').click(function() {
            $("[id$='CheckReject']").attr('checked', false);
        });
    }
</script>