【问题标题】:jQuery for CheckAll CheckBox in GridView column headerjQuery 用于 GridView 列标题中的 CheckAll CheckBox
【发布时间】:2013-03-25 17:53:35
【问题描述】:

我们有一个包含多列的 GridView。其中两列包含 CheckBoxes,而其余列包含 TextBoxes 或 DropDownLists。

放置 GridView 的表单嵌入在母版页中。

在标题行中使用 CheckBox,我们希望将最后一列中的所有复选框设置为标题中 CheckBox 的状态。我们不想设置第 4 列中的复选框。 CheckBox 的 Id 为“chkUpdate”

我见过的示例每行只有 1 个复选框并使用 CSS 类来识别它,但是我们 GridView 行上的两个复选框都使用相同的 CSS 类,并且必须创建一个新的 CSS 类来纯粹识别它似乎是错误的不同的复选框列

我知道我可以在 GridView 的行上使用 each 循环,但无法确定如何识别最后一列中的复选框

function checkAll(objRef) {
    $("#<%=gv_Vials.ClientID %> tr").each(function() {
    //What goes here? = objRef.checked;
    };
}

我希望我已经解释了我的要求,但如果有人需要进一步说明,请随时询问

【问题讨论】:

  • 我会完全按照您说的去做,为您想要的复选框创建一个单独的类。然后使用 jQuery 选择器来访问它们。
  • 无耻的自我推销:there's a jQuery plugin for that.

标签: jquery asp.net gridview


【解决方案1】:

基本上,您可以使用带有选择器的 jQuery 结尾 - id*="chkSelected"。它将选中所有以chkSelected结尾的复选框

<asp:GridView runat="server" ID="gv_Vials">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                <input id="btnCheckAll" type="checkbox" name="AllCheck" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="chkSelected" Checked='<%# bool.Parse(Eval("IsActive").ToString()) %>'/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<script>
    var checkBoxSelector = '#<%=gv_Vials.ClientID%> input[id*="chkSelected"]:checkbox';

    $(document).ready(function () {
        $('#btnCheckAll').live('click', function () {
            if ($('#btnCheckAll').is(':checked')) {
                $(checkBoxSelector).attr('checked', true);
            }
            else {
                $(checkBoxSelector).attr('checked', false);
            }
        });
    });
</script>

【讨论】:

  • 这种方法的“错误”是它要求您在 Gridview ID 中进行硬编码。有没有办法在没有硬编码的情况下做同样的事情?
【解决方案2】:

我就是这样做的。

ASP.net:

            <asp:GridView ID="gvStudents" runat="server" DataSourceID="SqlDataSourceStudents" AutoGenerateColumns="False" Width="100%" OnRowDataBound="gvStudents_RowDataBound">
                <HeaderStyle BackColor="#5D7B9D" ForeColor="White" />
                <AlternatingRowStyle BackColor="#EEEEEE" />
                <RowStyle BackColor="White" />
                <Columns>
**..normal column templates or boundfields go here..**
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:CheckBox id="CheckBoxAll" runat="server" onclick="javascript:SelectAllCheckboxesCol(this);"/>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBoxAdd" runat="server" onclick="javascript:HighlightRow(this);"/>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

jQuery:

        //jQuery to select all checkboxes on the last column (4th column) of gvStudents
        function SelectAllCheckboxesCol(chk) 
        {
            $('#<%=gvStudents.ClientID %> >tbody >tr >td:nth-child(4) > input:checkbox').attr('checked', chk.checked);

            //this works but there must be a better way! jQuery is not my fortae yet :)
            cBox.attr('checked', chk.checked);  //check all the checkboxes
            cBox.click();                       //click them all to fire the Highlightrow() function. This un-ticks the checkboxes!
            cBox.attr('checked', chk.checked);  //re-check them again!
        }

        //jQuery to highlight a row selected
        function HighlightRow(chk) {
            var isChecked = chk.checked;
            var $selectedRow = $("#" + chk.id).parent("td").parent("tr");
            var selectedIndex = $selectedRow[0].rowIndex;
            var sColor = '';

            if(selectedIndex%2 == 0)
                sColor = '#EEEEEE';
            else
                sColor = 'white';

            if(isChecked)
                $selectedRow.css({
                    "background-color" : "Yellow",
                    "color" : "Black"
                });
            else
                $selectedRow.css({
                    "background-color" : sColor,
                    "color" : "Black"
                });
        }
}

具有以上特点的有:

  • 后面没有代码
  • 同时高亮一行,恢复原来的Alternate row背景
  • 仅选择特定列和特定 Gridview 的复选框

缺点:

  • 它要求您硬编码 Gridview 的名称并指定列号

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多