日期:2014-05-20 浏览次数:20557 次
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeViewSample.aspx.cs" Inherits="WebApp.TreeViewSample" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>treeview父子节点联动</title> <script type="text/javascript"> function OnTreeNodeChecked(evt) { var objNode = evt.srcElement ? evt.srcElement : evt.target; if (objNode.checked == true) { alert(getHideValue(objNode)); setParentChecked(objNode); var objID = objNode.getAttribute("ID"); var objID = objID.substring(0, objID.indexOf("CheckBox")); var objParentDiv = document.getElementById(objID + "Nodes"); if (objParentDiv != null && objParentDiv != "undefined") { setChildChecked(objParentDiv); } } else { var objID = objNode.getAttribute("ID"); var objID = objID.substring(0, objID.indexOf("CheckBox")); var objParentDiv = document.getElementById(objID + "Nodes"); if (objParentDiv != null && objParentDiv != "undefined") { setChildUnChecked(objParentDiv); } } if (objNode.tagName && objNode.tagName != "INPUT") { if (objNode.tagName == "IMG") { return true; } else { if (window.event) { window.event.returnValue = false; return false; } else { evt.preventDefault(); evt.stopPropagation(); } } } } function public_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 = public_GetParentByTagName(objNode, "div"); if (objParentDiv == null || objParentDiv == "undefined") { return; } var objID = objParentDiv.getAttribute("ID"); objID = objID.substring(0, objID.indexOf("Nodes")); objID = objID + "CheckBox"; var objParentCheckBox = document.getElementById(objID); if (objParentCheckBox == null || objParentCheckBox == "undefined") { return; } if (objParentCheckBox.tagName != "INPUT" && objParentCheckBox.type == "checkbox") return; objParentCheckBox.checked = true; setParentChecked(objParentCheckBox); } function setChildUnChecked(divID) { var objchild = divID.children; 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 = false; } setChildUnChecked(tempObj); } } function setChildChecked(divID) { var objchild = divID.children; 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 = true; } setChildChecked(tempObj); } } function getHideValue(node) { var objchild = public_GetParentByTagName(node, "td").getElementsByTagName("input"); for (var i = 0; i < objchild.length; i++) { var tempObj = objchild[i]; if (tempObj.tagName == "INPUT" && tempObj.type == "hidden") { alert(tempObj.value); } } } </script> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:TreeView ID="TreeView1" runat="server"> </asp:TreeView> </form> </body> </html>