Using JS script to control the Nodecheck implementation code _javascript techniques of asp.net under the TreeView

Source: Internet
Author: User
Tags tagname
Increase the checkbox linkage situation selection:
1. Any node in the radio TreeView
2. When a node CheckBox property value changes: The child node's CheckBox property value follows its changes, the parent node does not change;
False when the CheckBox property value is false for all child nodes of the parent node, or True if the CheckBox property value of a child node is true.
3. When a node CheckBox property value changes: Child nodes, the parent Node CheckBox property values follow its changes;
False when the CheckBox property value is false for all child nodes of the parent node, or True if the CheckBox property value of a child node is true.
JavaScript code
Copy Code code as follows:

function ontreenodechecked (ID, type) {
Gets the object that triggered the event
var element = Window.event.srcElement;
Does not handle if the object is not a checkbox
if (! Ischeckbox (Element))
Return
Get checked status
var ischecked = element.checked;
Get Tree Object
var tree = Tv2_gettreebyid (ID);
Gets the relative node of the element (if it is a leaf node, it is an element, otherwise its <A> node)
var node = Tv2_getnode (tree, Element);
Switch (type) {
Case "1":
Setnodesunchecked (tree);
Element.checked = true;
Break
Case "2":
Tv2_setchildnodescheckstatus (node, ischecked);
Break
Case "3":
Tv2_setchildnodescheckstatus (node, ischecked);
var parent = Tv2_getparentnode (tree, node);
Tv2_nodeonchildnodecheckedchanged (tree, parent, ischecked);
}
}
Set all nodes checkbox nochecked
function setnodesunchecked (TreeNode) {
var inputs = Webform_getelementsbytagname (TreeNode, "INPUT");
if (inputs = NULL | | inputs.length = 0)
Return
for (var i = 0; i < inputs.length; i++) {
if (Ischeckbox (Inputs[i]))
inputs[i].checked = false;
}
}
Set Child nodes checkbox status
function tv2_setchildnodescheckstatus (node, ischecked) {
Returns the div layer where the current node is located
var childnodes = tv2i_getchildnodesdiv (node);
if (childnodes = null)
Return
var inputs = Webform_getelementsbytagname (ChildNodes, "INPUT");
if (inputs = NULL | | inputs.length = 0)
Return
for (var i = 0; i < inputs.length; i++) {
if (Ischeckbox (Inputs[i]))
inputs[i].checked = ischecked;
}
}
Change parent node checkbox status after child node changed
function tv2_nodeonchildnodecheckedchanged (tree, node, ischecked) {
if (node = null)
Return
var childnodes = tv2_getchildnodes (tree, node);
if (childnodes = null | | childnodes.length = 0)
Return
var isallsame = true;
for (var i = 0; i < childnodes.length; i++) {
var item = Childnodes[i];
var value = tv2_nodegetchecked (item);
if (ischecked!= value) {
Isallsame = false;
Break
}
}
var parent = Tv2_getparentnode (tree, node);
if (isallsame) {
tv2_nodesetchecked (node, ischecked);
Tv2_nodeonchildnodecheckedchanged (tree, parent, ischecked);
}
else {
Tv2_nodesetchecked (node, true);
Tv2_nodeonchildnodecheckedchanged (tree, parent, true);
}
}
Get node relative element (etc. checkbox)
function Tv2_getnode (tree, Element) {
var id = element.id.replace (tree.id, "");
id = id.tolowercase (). Replace (Element.type, "");
ID = tree.id + ID;
var node = document.getElementById (ID);
if (node = null)//leaf node, no "A" node
return element;
return node;
}
Get parent Node
function Tv2_getparentnode (tree, node) {
var div = webform_getparentbytagname (node, "div");
The structure of node: <table>information of Node</table><div>child nodes</div>
var table = div.previoussibling;
if (table = = null)
return null;
Return tv2i_getnodeinelement (tree, table);
}
Get child nodes Array
function tv2_getchildnodes (tree, node) {
if (tv2_nodeisleaf (node))
return null;
var children = new Array ();
var div = tv2i_getchildnodesdiv (node);
var index = 0;
for (var i = 0; i < div.childNodes.length; i++) {
var element = Div.childnodes[i];
if (element.tagname!= "TABLE")
Continue
var child = Tv2i_getnodeinelement (tree, Element);
if (Child!= null)
children[index++] = child;
}
return children;
}
function tv2_nodeisleaf (node) {
Return! (Node.tagname = = "A"); Todo
}
function tv2_nodegetchecked (node) {
var checkbox = Tv2i_nodegetcheckbox (node);
return checkbox.checked;
}
function tv2_nodesetchecked (node, ischecked) {
var checkbox = Tv2i_nodegetcheckbox (node);
if (checkbox!= null)
checkbox.checked = ischecked;
}
function Ischeckbox (Element) {
if (element = = null)
return false;
return (Element.tagname = = "INPUT" && element.type.toLowerCase () = = "checkbox");
}
Get tree
function Tv2_gettreebyid (ID) {
return document.getElementById (ID);
}
//////////////////////////////////////////////////////////////////////////////////////////////
Private mothods, with tv2i_ prefix
//////////////////////////////////////////////////////////////////////////////////////////////
Get DIV contains child nodes
function Tv2i_getchildnodesdiv (node) {
if node.tagname = = "A" is not processed
if (tv2_nodeisleaf (node))
return null;
var childnodsdivid = node.id + "Nodes";
return document.getElementById (Childnodsdivid);
}
Find node in element
function tv2i_getnodeinelement (tree, Element) {
var node = Tv2i_getnodeinelementa (tree, Element);
if (node = null) {
node = Tv2i_getnodeinelementinput (tree, Element);
}
return node;
}
Find "A" node
function Tv2i_getnodeinelementa (tree, Element) {
var as = Webform_getelementsbytagname (element, "A");
if (as = = NULL | | as.length = 0)
return null;
var regexp = new RegExp ("^" + tree.id + "n\\d+$");
for (var i = 0; i < as.length; i++) {
if (As[i].id.match (regexp)) {
return as[i];
}
}
return null;
}
Find "INPUT" node
function tv2i_getnodeinelementinput (tree, Element) {
var as = Webform_getelementsbytagname (element, "INPUT");
if (as = = NULL | | as.length = 0)
return null;
var regexp = new RegExp ("^" + tree.id + "n\\d+");
for (var i = 0; i < as.length; i++) {
if (As[i].id.match (regexp)) {
return as[i];
}
}
return null;
}
Get checkbox of Node
function Tv2i_nodegetcheckbox (node) {
if (Ischeckbox (node))
return node;
var id = node.id + "CheckBox";
return document.getElementById (ID);
}

HTML code
Copy Code code as follows:

<asp:treeview id= "TreeView1" runat= "Server" imageset= "MSDN" Showcheckboxes= "All"
showlines= "True" borderwidth= "0px" height= "430px" width= "250px" font-size= "Small"
Onclick= "ontreenodechecked ()" >
</asp:TreeView>
Related Article

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.