【转】Asp.Net TreeView with CheckBox

时间:2022-02-06 08:52:32

1  选择了父节点,则它的所有子节点都选中
2 只要有一个子节点选中, 其父节点就选中

<script language="javascript" type="text/javascript">
function HandleCheckbox()                                           
{                                                                              
    var element = event.srcElement;                                            
    if (element.tagName == "INPUT" && element.type == "checkbox")              
    {                                                                          
        var checkedState = element.checked;                                   
        while (element.tagName != "TABLE") // Get wrapping table               
        {                                                                      
            element = element.parentElement;                                   
        }                                                                            
        var parentElement = element;
                   
        if(checkedState)
        {
         CheckParents(element);
        } 
                                                                                     
        element = element.nextSibling; //element.tagName = DIV 
                                                                              
        if (element != null) // If no childrens then exit 
        {                                                                                                     
            var childTables = element.getElementsByTagName("TABLE"); 
            
            for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++)
            {                                                                      
                CheckTable(childTables[tableIndex], checkedState);      
            } 
        }
        if(checkedState == false)
        {
            UnCheckParents(parentElement);
        }            
                                                            
    }                                                                          
}                                                                              
                                                                               
// Uncheck the parents of the given table, Can remove the recurse (redundant) 
function CheckParents(table)                                      
{                                                                              
    if (table == null || table.rows[0].cells.length == 2) // This is the root 
    {                                                                          
        return;                                                                
    }                                                                          
    var parentTable = table.parentElement.previousSibling;
    CheckTable(parentTable, true);                                 
    CheckParents(parentTable);                                    
}  

// Check the parents of the given table, Can remove the recurse (redundant) 
function UnCheckParents(table)                                      
{       
                                                                     
    if (table == null || table.rows[0].cells.length == 2) // This is the root  
    {                                                                          
        return;                                                                
    }                                                                    
    var parentTable = table.parentElement.previousSibling; 
   
    var checkedCount  =GetCheckedCount(table.parentElement);
    if(checkedCount == 0)
    {
        CheckTable(parentTable, false);
    }                                                 
    UnCheckParents(parentTable);                                    
}                                                                              
                                                                               
// Handle the set of checkbox checked state                                    
function CheckTable(table, checked) 
    {                                                                              
    var checkboxIndex = table.rows[0].cells.length - 1;   
    var cell = table.rows[0].cells[checkboxIndex];  
    var checkboxes = cell.getElementsByTagName("INPUT"); 
    if (checkboxes.length == 1) 
    {                                                                          
        checkboxes[0].checked = checked; 
    } 
                                                                           
}  
//Get checked children count
function GetCheckedCount(table)
{
    var checkedCount = 0; 
    var element = table.nextSibling;
    var childTable = table.getElementsByTagName("TABLE");
    
    for(var tableIndex = 0;tableIndex < childTable.length; tableIndex++)
    {
        var childTables = childTable[tableIndex];
        var checkboxIndex = childTables.rows[0].cells.length - 1;    
        var cell = childTables.rows[0].cells[checkboxIndex];  
        var checkboxes = cell.getElementsByTagName("INPUT");
        if (checkboxes.length == 1 && checkboxes[0].checked == true)
        {                                                                          
             checkedCount++;            
        }      
    }   
    return  checkedCount;   
}                                                                            
 </script>

<asp:TreeView ID="TreeViewModules" runat="server" ShowCheckBoxes="All" onclick="HandleCheckbox();"
        ShowLines="True" ExpandDepth="1" meta:resourcekey="TreeViewModulesResource1"></asp:TreeView>