选中gridview中所有行的特定列的所有复选框

Check all the checkboxes of a particular column of all rows in gridview

本文关键字:复选框 gridview 选中      更新时间:2023-09-26

我创建了一个gridview,其中包含1个隐藏的boundfield, 1个boundfield和动态创建的模板字段,其中包含HeaderTemplate以及ItemTemplate中的复选框。没有。

复选框列的

复选框的ID是通过命名容器生成的,如Hearder复选框ID为"gvempsalarystructure_respectivecolumnname"项目复选框ID是"gvempsalarystructure_respectivecolumnname_incrementingindex(从0开始)"

现在通过选择一个列的标题复选框,该列的所有复选框都应该被选中/取消选中。

如何通过javascript或任何更好的方式实现这一点。[注:我对javascript一窍不通]

该链接包含网格视图的图像,请查看参考。动态创建gridview与复选框

可以。假设CheckBox的ID看起来像gvEmpSalaryStructure_columnName_2,其中2是列索引。

<script type="text/javascript">
    $("#<%= GridView1.ClientID %> th input:checkbox").change(function () {
        var columnIndex = this.id.split("_")[2];
        changeAllCheckBoxes(this.checked, columnIndex);
    });
    function changeAllCheckBoxes(is_checked, columnIndex) {
        $("#<%= GridView1.ClientID %> tr").each(function () {
            $(this).find("td input:checkbox").each(function (index, element) {
                if (index == columnIndex) {
                    this.checked = is_checked;
                }
            });
        });
    }
</script>
$(".colmnHeader").on('click',function(){
    var getTDPosition= this.cellIndex;
    $("#table tr").each(function(){ 
        // get checkbox for specific colmn and make it checked
    });
});