Front-end code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Capture events on the client
Function CheckEvent (){
Var objNode = event. srcElement;
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 whether other nodes in parallel are selected
Function HasOtherChecked (objNode ){
Var objParentDiv = WebForm_GetParentByTagName (objNode, "div ");
Var chks = objParentDiv. getElementsByTagName ("INPUT ");
For (var I = 0; I <chks. length; I ++ ){
If (chks [I]. checked & chks [I]. id! = ObjNode. id ){
Return true;
}
}
Return false;
}
// Set the 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 the subnode
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>
<Body>
<Form id = "form1" runat = "server">
<Asp: TreeView ID = "TreeView1" runat = "server" ShowCheckBoxes = "All" onclick = "CheckEvent ()">
</Asp: TreeView>
</Form>
</Body>
Background code:
Copy codeThe Code is as follows:
Private void BindGrid ()
{
DataTable mytable = menuTableBll. GetAllList (). Tables [0]; // obtain data
CreateTree (mytable, TreeView1.Nodes, "0 ");
}
Private void CreateTree (DataTable dtNodeSets, TreeNodeCollection node, string parent_id)
{
DataView dvList = new DataView (dtNodeSets );
DvList. RowFilter = "MeunParentId =" + parent_id;
TreeNode nodeTemp;
Foreach (DataRowView dv in dvList)
{
NodeTemp = new TreeNode ();
NodeTemp. Text = dv ["MenuName"]. ToString ();
NodeTemp. Expanded = true;
NodeTemp. ToolTip = dv ["MeunId"]. ToString ();
Node. Add (nodeTemp );
CreateTree (dtNodeSets, nodeTemp. ChildNodes, dv ["MeunId"]. ToString ());
}
}