<script type="text/javascript"> </script> <html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"> <title>无标题页</title> <script type="text/javascript"> function OnTreeNodeChecked() { var ele = window.event.srcElement; if(ele.type=='checkbox') //如果ele是复选框 { var childrenDivID = ele.id.replace('CheckBox','Nodes'); //通过替换造divid var div = document.getElementById(childrenDivID); //查找该div是否存在 if(div != null) //如果div存在,则表示ele是某些节点的父节点 { var checkBoxs = div.getElementsByTagName('INPUT'); //查找该父节点下所有的复选框 for(var i=0;i<checkBoxs.length;i++) { if(checkBoxs[i].type=='checkbox') checkBoxs[i].checked=ele.checked; } } else { var div = GetParentByTagName(ele,'DIV'); var checkBoxs = div.getElementsByTagName('INPUT'); var parentCheckBoxID = div.id.replace('Nodes','CheckBox'); var parentCheckBox = document.getElementById(parentCheckBoxID); for(var i=0;i<checkBoxs.length;i++) { if(checkBoxs[i].type=='checkbox' && checkBoxs[i].checked) { parentCheckBox.checked = true; return; } } parentCheckBox.checked = false; } } } function GetParentByTagName(element, tagName) { var parent = element.parentNode; var upperTagName = tagName.toUpperCase(); while (parent && (parent.tagName.toUpperCase() != upperTagName)) { parent = parent.parentNode ? parent.parentNode : parent.parentElement; } return parent;} </script></head> <body> <form id="form1" runat="server"> <div> <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" οnclick="OnTreeNodeChecked()"> <Nodes> <asp:TreeNode SelectAction="Expand" Text="aaa" Value="aaa"> <asp:TreeNode SelectAction="None" Text="a1" Value="a1"></asp:TreeNode> <asp:TreeNode SelectAction="None" Text="a2" Value="a2"></asp:TreeNode> </asp:TreeNode> <asp:TreeNode SelectAction="Expand" Text="bbb" Value="bbb"> <asp:TreeNode SelectAction="None" Text="b1" Value="b1"></asp:TreeNode> <asp:TreeNode SelectAction="None" Text="b2" Value="b2"></asp:TreeNode> </asp:TreeNode> < /Nodes> </asp:TreeView> </div> </form></body></html>