1, in Page_Load (object sender, EventArgs e) added: TreeView1.Attributes.Add ("onclick", "Checkevent (event)");// TreeView1 is the name of the tree control.
JavaScript placed between
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<script type= "Text/javascript" >
function Checkevent (evt) {
EVT = Window.event | | Evt
var objnode = Evt.srcelement | | Evt.target;
if (Objnode.tagname = = "INPUT" && objnode.type = = "checkbox") {
var objparentdiv = objNode.id.replace ("checkbox", "Nodes");
if (objnode.checked = = True) {
Setchildcheckstate (Objparentdiv, true);
setparentcheckestate (Objnode, true);
}
else {
Setchildcheckstate (Objparentdiv, false);
if (! Hasotherchecked (Objnode)) {
Setparentcheckestate (Objnode, false);
}
}
}
}
Determine if there are other nodes in parallel that are selected
function hasotherchecked (objnode) {
var objparentdiv = Webform_getparentbytagname (Objnode, "div");
var chks = Objparentdiv.getelementsbytagname ("INPUT");
for (var i = 0; i < chks.length; i++) {
& nbsp; if (chks[i].checked && chks[i].id! = objnode.id) {
return true;
}
}
return false;
}
/Set parent node
function Setparentcheckestate (Objnode, chkstate) {
try {
var Objparentdiv = Webform_getparentbytagname (Objnode, "div");
if (Objparentdiv = = null | | Objparentdiv = = "undefined") {
return;
}
else {
var objparentchkid = objParentDiv.id.replace ("Nodes", "checkbox");
var objparentcheckbox = document.getElementById (objparentchkid);
if (objparentcheckbox) {
objparentcheckbox.checked = chkstate;
setparentcheckestate (Objparentdiv, chkstate);
}
}
}
catch (e) {}
}
/Set child nodes
function Setchildcheckstate (Nodeid, chkstate) {
var node = document.getElementById (Nodeid);
if (node) {
var chks = node.getelementsbytagname ("INPUT");
for (var i = 0; i < chks.length; i++) {
chks[i].checked = chkstate;
}
}
}
</script>
2. Registering events in Page_Load
protected void Page_Load (object sender, EventArgs e) {
TREEVIEW1.ATTRIBUTES.ADD ("onclick", "Checkevent (event)");
}
The net implementation of the TreeView selects the parent node whose child nodes are also selected by selecting a neutron node whose parent node and root node are also chosen