< div > < script language ="javascript" type ="text/javascript" > ... function OnTreeNodeChecked() ...{ var ele = window.event.srcElement; if(ele.type=='checkbox') ...{ var childrenDivID = ele.id.replace('CheckBox','Nodes'); var div = document.getElementById(childrenDivID); if(div==null)return; var checkBoxs = div.getElementsByTagName('INPUT'); for(var i=0;i<checkBoxs.length;i++) ...{ if(checkBoxs[i].type=='checkbox') checkBoxs[i].checked=ele.checked; } } }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; } function setParentChecked(objNode) ...{ var objParentDiv = getParentByTagName(objNode,"div"); if(objParentDiv == null || objParentDiv == "undefined") return; var objID = objParentDiv.getAttribute("ID"); var objParentCheckBox = document.getElementById(objID.replace("Nodes","CheckBox")); if(objParentCheckBox == null || objParentCheckBox == "undefined") return; if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox") return; objParentCheckBox.checked = true; setParentChecked(objParentCheckBox); } function setChildCheckedState(div,state) ...{ var objchild = div.childNodes; var count = objchild.length; for(var i=0;i<objchild.length;i++) ...{ var tempObj = objchild[i]; if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox") ...{ tempObj.checked = state; } setChildCheckedState(tempObj,state); } } function TreeNodeChecked() ...{ var objNode = window.event.srcElement; if(objNode.tagName!="INPUT" || objNode.type!="checkbox") return; if(objNode.checked == true) ...{// alert('setParentChecked'); setParentChecked(objNode); } var objID = objNode.getAttribute("ID"); var objParentDiv = document.getElementById(objID.replace("CheckBox","Nodes")); if(objParentDiv==null || typeof(objParentDiv) == "undefined") return; setChildCheckedState(objParentDiv,objNode.checked); } </ script > < asp:TreeView ID ="TreeView1" runat ="server" ShowCheckBoxes ="All" > </ asp:TreeView ></ div > 在后台添加代码 TreeView1.Attributes.Add("onclick", "TreeNodeChecked();");