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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.