gridview中Checkbox全选或全取消的js代码

    技术2022-05-19  30

     

    js如下:

     

    function CheckAll(id,GridViewID) {            //get reference of GridView control       var grid = document.getElementById("<%= GridViewID.ClientID %>");            //variable to contain the cell of the grid

                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                       // alert(cell.childNodes[j].type);//                        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;                        //                        }

                                control=cell.childNodes[j];                                    for(d=0; d<3;d++) //search up to 3 levels deep                                    {                                          //if childNode type is CheckBox                                                                  if (control.type =="checkbox")                                          {                                          //assign the status of the Select All checkbox to the cell checkbox within the grid                                                control.checked = document.getElementById(id).checked;                                                break; //once you find it get out!!                                          }                                        if(control.childNodes.length>0)                                                      control=control.childNodes[0];                                    }

                        }                }            }        }       C#代码加入

            protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)        {              if (e.Row.RowType == DataControlRowType.Header)             {                 e.Row.Cells[0].Text = "<input id=/"Checkbox2/" type=/"checkbox/" name=/"chkall/" οnclick=/"CheckAll('Checkbox2')/"/><label></label>";             }        }


    最新回复(0)