问题描述
使用TreeView都是checkbox结构如何选择父节点后使子节点也自动选上
解决方案
解决方案二:
遍历子节点喽
解决方案三:
privatevoidtreeView1_Check(objectsender,TreeViewEventArgse){if(e.Node.Nodes.Count>0){foreach(TreeNodenodeine.Node.Nodes){node.Checked=e.Node.Checked;}}}
解决方案四:
用递归在AfterCheck中加上selectnode(e.Node,e.Node.Checked)PrivateSubselectnode(ByValnAsTreeNode,ByValckAsBoolean)IfNot(n.Nodes.Count=0)Thenn=n.LastNodeWhileNot(nIsNothing)n.Checked=ckselectnode(n,ck)n=n.PrevNodeEndWhileEndIfEndSub
解决方案五:
这个必须你在服务器端自行写事件处理来完成,没有自动化的实现。
解决方案六:
服务器:TreeView1.Attributes.Add("OnClick","OnTreeNodeChecked(event)");客户端脚本如下:/***************************************************************************************Name:ClientJavascriptforASP.NET2.0TreeViewDescription:ASP.NET2.0TreeViewlackforclientoperation.Thissetoffunctionsprovidesomesupports.Includes:*getnode*changecheckboxstatusofparentandchildnodesAuthor:Zhangtao,zhangtao.it@gmail.comDate:2006-03-30Commonts:***************************************************************************************/functionTV2_SetChildNodesCheckStatus(node,isChecked){varchildNodes=TV2i_GetChildNodesDiv(node);if(childNodes==null)return;varinputs=WebForm_GetElementsByTagName(childNodes,"INPUT");if(inputs==null||inputs.length==0)return;for(vari=0;i<inputs.length;i++){if(IsCheckBox(inputs[i]))inputs[i].checked=isChecked;}}functionTV2_NodeOnChildNodeCheckedChanged(tree,node,isChecked){if(node==null)return;varchildNodes=TV2_GetChildNodes(tree,node);if(childNodes==null||childNodes.length==0)return;varisAllSame=true;for(vari=0;i<childNodes.length;i++){varitem=childNodes[i];varvalue=TV2_NodeGetChecked(item);if(isChecked!=value){isAllSame=false;break;}}varparent=TV2_GetParentNode(tree,node);if(isAllSame){TV2_NodeSetChecked(node,isChecked);TV2_NodeOnChildNodeCheckedChanged(tree,parent,isChecked);}else{TV2_NodeSetChecked(node,false);TV2_NodeOnChildNodeCheckedChanged(tree,parent,false);}}functionTV2_GetNode(tree,element){varid=element.id.replace(tree.id,"");id=id.replace(element.type,"");id=tree.id+id;varnode=document.getElementById(id);if(node==null)//leafnode,no"A"nodereturnelement;returnnode;}functionTV2_GetParentNode(tree,node){vardiv=WebForm_GetParentByTagName(node,"DIV");//Thestructureofnode:<table>informationofnode</table><div>childnodes</div>vartable=div.previousSibling;if(table==null)returnnull;returnTV2i_GetNodeInElement(tree,table);}functionTV2_GetChildNodes(tree,node){if(TV2_NodeIsLeaf(node))returnnull;varchildren=newArray();vardiv=TV2i_GetChildNodesDiv(node);varindex=0;for(vari=0;i<div.childNodes.length;i++){varelement=div.childNodes[i];if(element.tagName!="TABLE")continue;varchild=TV2i_GetNodeInElement(tree,element);if(child!=null)children[index++]=child;}returnchildren;}functionTV2_NodeIsLeaf(node){return!(node.tagName=="A");//Todo}functionTV2_NodeGetChecked(node){varcheckbox=TV2i_NodeGetCheckBox(node);returncheckbox.checked;}functionTV2_NodeSetChecked(node,isChecked){varcheckbox=TV2i_NodeGetCheckBox(node);if(checkbox!=null)checkbox.checked=isChecked;}functionIsCheckBox(element){if(element==null)returnfalse;return(element.tagName=="INPUT"&&element.type.toLowerCase()=="checkbox");}functionTV2_GetTreeById(id){returndocument.getElementById(id);}functionTV2i_GetChildNodesDiv(node){if(TV2_NodeIsLeaf(node))returnnull;varchildNodsDivId=node.id+"Nodes";returndocument.getElementById(childNodsDivId);}functionTV2i_GetNodeInElement(tree,element){varnode=TV2i_GetNodeInElementA(tree,element);if(node==null){node=TV2i_GetNodeInElementInput(tree,element);}returnnode;}functionTV2i_GetNodeInElementA(tree,element){varas=WebForm_GetElementsByTagName(element,"A");if(as==null||as.length==0)returnnull;varregexp=newRegExp("^"+tree.id+"n\d+$");for(vari=0;i<as.length;i++){if(as[i].id.match(regexp)){returnas[i];}}returnnull;}functionTV2i_GetNodeInElementInput(tree,element){varas=WebForm_GetElementsByTagName(element,"INPUT");if(as==null||as.length==0)returnnull;varregexp=newRegExp("^"+tree.id+"n\d+");for(vari=0;i<as.length;i++){if(as[i].id.match(regexp)){returnas[i];}}returnnull;}functionTV2i_NodeGetCheckBox(node){if(IsCheckBox(node))returnnode;varid=node.id+"CheckBox";returndocument.getElementById(id);}functionOnTreeNodeChecked(){varele=event.srcElement;if(ele.type=='checkbox'){varchildrenDivID=ele.id.replace('CheckBox','Nodes');vardiv=document.getElementById(childrenDivID);if(div==null)return;varcheckBoxs=div.getElementsByTagName('INPUT');for(vari=0;i<checkBoxs.length;i++){if(checkBoxs[i].type=='checkbox')checkBoxs[i].checked=ele.checked;}}}
解决方案七:
遍历,判断,处理,LSjs强
解决方案八:
二楼的不行,,你那样只能选择第二级的菜单,,第三级以后的都不会选到//下面这个方法是树的点击事件privatevoidTreeView_AfterCheck(objectsender,TreeViewEventArgse){if(e.Action!=TreeViewAction.Unknown){if(e.Node.Nodes.Count>0){pCheckAllChildNodes(e.Node,e.Node.Checked);}pCheckParentNodes(e.Node);}}//选择父节点privatevoidpCheckParentNodes(TreeNodetreeNode){TreeNode_oParentNode=treeNode.Parent;while(_oParentNode!=null){bool_bChecked=false;for(int_k=0;_k<_oParentNode.Nodes.Count;_k++){if(_oParentNode.Nodes[_k].Checked!=_bChecked){_bChecked=!_bChecked;break;}}_oParentNode.Checked=_bChecked;_oParentNode=_oParentNode.Parent;}}//选择子节点privatevoidpCheckAllChildNodes(TreeNodetreeNode,boolnodeChecked){foreach(TreeNodenodeintreeNode.Nodes){node.Checked=nodeChecked;if(node.Nodes.Count>0){pCheckAllChildNodes(node,nodeChecked);}}}