【问题标题】:Asp.net multiple gridviews, checkboxes in headertemplate select all / deselect all functionalityAsp.net多个gridviews,headertemplate中的复选框全选/取消全选功能
【发布时间】:2011-12-01 06:02:09
【问题描述】:

我在我的 aspx 页面中使用了两个 gridview 控件。而且我的两个控件中都有一列复选框。我通过在两个网格视图的标题模板中提供一个复选框并使用 java 脚本函数来帮助用户选择/取消选择我的两个网格视图中的所有复选框。下面是代码

<script type="text/javascript">
    function UncheckParent(obj) {
        var isUnchecked = obj.checked;
        if (isUnchecked == false) {
            var frm = document.forms[0];
            for (i = 0; i < frm.length; i++) {
                if (frm.elements[i].id.indexOf('chkSelectAllCheckboxes') != -1) {
                    frm.elements[i].checked = false;
                }
            }
        }

    }

    function CheckAll(obj) {
        var row = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        var inputs = row.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "checkbox") {
                inputs[i].checked = obj.checked;
            }
        }
    }
</script>

但问题是,一旦我选择或取消选择此类复选框之一,两个网格视图中的所有复选框都会被选中。我还可以举一个简短的例子来说明正在发生的事情。两个网格视图 gdvwStudents 和 gdvwTeachers。两者都有复选框列和标题模板中的复选框。当我单击 gdvwStudents 的标题复选框时使用上面的代码,gdvStudents 和 gdvTeachers 中的所有复选框都被选中。请

【问题讨论】:

    标签: javascript asp.net c#-4.0


    【解决方案1】:

    你做错了。您应该在单击标题的特定网格内获得复选框;相反,您将获得ALL 在表单上创建的复选框!这非常低效,并解释了为什么无论您点击哪个标题,所有内容都会被选中/取消选中。

    如果你可以使用 jQuery,你应该可以将这些函数重写成这样的:

    function checkAll(gridID,checkbox) {
        $("#"+gridID+" input:checkbox").attr("checked",checkbox.checked);
    }
    

    请参阅this jsfiddle 以获取快速示例。

    【讨论】:

      【解决方案2】:

      如下添加javascript函数

       <script type="text/javascript">
       function SelectAll(id, grd) {
           //get reference of GridView control
           var grid = null;
           if (grd = "1") {
               grid = document.getElementById("<%= GridView1.ClientID %>");
           }
           else {
               grid = document.getElementById("<%= GridView2.ClientID %>");
           }
      
      
           //variable to contain the cell of the grid
           var cell;
      
           if (grid.rows.length > 0) {
               //loop starts from 1. rows[0] points to the header.
               for (i = 1; i < grid.rows.length; i++) {
                   //get the reference of first column
                   cell = grid.rows[i].cells[0];
      
                   //loop according to the number of childNodes in the cell
                   for (j = 0; j < cell.childNodes.length; j++) {
                       //if childNode type is CheckBox                 
                       if (cell.childNodes[j].type == "checkbox") {
                           //assign the status of the Select All checkbox to the cell checkbox within the grid
                           cell.childNodes[j].checked = document.getElementById(id).checked;
                       }
                   }
               }
           }
       }
      </script>   
      

      并按以下方式处理 javascript 函数的 gridviews 绑定调用的 Rowbound 事件

         protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
          {
              if (e.Row.RowType == DataControlRowType.Header)
              { 
                  //Find the checkbox control in header and add an attribute
                  ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + 
                          ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "'" + ", '1')" );
              }
          }
      
          protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
          {
              if (e.Row.RowType == DataControlRowType.Header)
              { 
                  //Find the checkbox control in header and add an attribute
                  ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + 
                          ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "'" + ", '2')" );
              }
          }
      

      【讨论】:

        【解决方案3】:

        这就是我的管理方式:

                function UnCheckAllContainer(obj) {
                var isUnchecked = obj.checked;
                if (isUnchecked == false) {
                    var frm = document.forms[0];
                    for (i = 0; i < frm.length; i++) {
                        if (frm.elements[i].id.indexOf('chkSelectAllCheckboxesContainers') != -1
                        ) {
                            frm.elements[i].checked = false;
                        }
                    }
                }
            }
        
            function CheckAllContainer(chk) {
                $('#<%=gdvwContainers.ClientID %>').find("input:checkbox").each(function () {
                    if (this != chk) {
                        this.checked = chk.checked;
                    }
                });
        
            }
        

        【讨论】:

          猜你喜欢
          • 2016-02-20
          • 1970-01-01
          • 1970-01-01
          • 2012-01-25
          • 1970-01-01
          • 1970-01-01
          • 2020-09-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多