asp.net 2.0 权限树的控制(多处转载)

           做权限的时候,主要实现如下功能
            1、该节点可以访问,则他的父节点也必能访问;
     2、该节点可以访问,则他的子节点也都能访问;
     3、该节点不可访问,则他的子节点也不能访问。
         使用带CheckBox的数型结构能得到很好的用户体验,可是编程的难度也是有点增加,如果全部有服务端来完成,那点下刷下,肯定不行,只能使用javascript,javascript调试的时候郁闷的很,一个字类,还有郁闷的递归,麻烦
我以前是使用以下该方法
None.gif < script language = " javascript " >
None.gif
<!--
None.gif        
// 初始化节点
None.gif
        initchecknode(document.all( " tvItemClientID " ).value,document.all( " checked " ).value)
None.gif        
None.gif            
// 初始化选中节点
None.gif
             function  initchecknode(tree,tvvalue)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
//获得需要初始化选择状态的节点的字符串.
InBlock.gif
                var selectedNodes = tvvalue;
InBlock.gif                
var arrayNodes = new Array();
InBlock.gif                arrayNodes 
= selectedNodes.split(",");
InBlock.gif                
var AllRootNode=new Array();
InBlock.gif                AllRootNode
=document.getElementById(tree).getChildren();
InBlock.gif                
//初始化选择的节点
InBlock.gif
                findAndCheckedNodes(AllRootNode,arrayNodes);
ExpandedBlockEnd.gif            }

None.gif
None.gif            
// 根据已选择的节点的字符串初始化选择的节点
None.gif
             function  findAndCheckedNodes(NodeArray,nodeDatas)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
//alert(NodeArray.length);
InBlock.gif
                 if(parseInt(NodeArray.length)==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                 
dot.gif{
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
for(var i=0;i<parseInt(NodeArray.length);i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
var cNode,nodeData;
InBlock.gif                        cNode
=NodeArray[i];
InBlock.gif                        
////如果该节点在nodeDatas里则初始化checked = true;
InBlock.gif
                        nodeData = cNode.getAttribute("NodeData");
InBlock.gif                        
for(var j=0;j<nodeDatas.length;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if(nodeDatas[j] == nodeData)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                cNode.setAttribute(
"checked","true");
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
//如果有子节点,则继续递归
InBlock.gif
                        if(parseInt(cNode.getChildren().length)!=0)
InBlock.gif                        findAndCheckedNodes(cNode.getChildren(),nodeDatas);    
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

None.gif
// -->
None.gif//
oncheck事件
None.gif
function  tree_oncheck(tree)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
var i;
InBlock.gif 
var node=tree.getTreeNode(tree.clickedNodeIndex);
InBlock.gif 
var Pchecked=tree.getTreeNode(tree.clickedNodeIndex).getAttribute("checked"); 
InBlock.gif setcheck(node,Pchecked);
InBlock.gif setParent(node,Pchecked);
InBlock.gif
//window.alert(Pchecked);
InBlock.gif
 document.all.checked.value = "";
InBlock.gif 
if (tree.getChildren().length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif    
for (i=0;i<=tree.getChildren().length-1;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (tree.getChildren()[i].getAttribute("Checked"))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             AddChecked(tree.getChildren()[i]);
ExpandedSubBlockEnd.gif        }
    
InBlock.gif        FindCheckedFromNode(tree.getChildren()[i]);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
// 设置子节点选中
None.gif

None.gif
function  setcheck(node,Pchecked)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
var i;
InBlock.gif 
var ChildNode=new Array();
InBlock.gif ChildNode
=node.getChildren();
InBlock.gif 
InBlock.gif 
if(parseInt(ChildNode.length)==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
return;
ExpandedSubBlockEnd.gif }

InBlock.gif 
else
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
for(i=0;i<ChildNode.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
var cNode;
InBlock.gif   cNode
=ChildNode[i];
InBlock.gif   cNode.setAttribute(
"Checked",Pchecked);
InBlock.gif   
//cNode.Checked = Pchecked;
InBlock.gif
   if(parseInt(cNode.getChildren().length)!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    setcheck(cNode,Pchecked);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
// 设置子节点选中/取消;
None.gif//
同时需要设置父节点的状态(如果是取消选中的话,仅仅设置本节点及其所有字接点,不涉及父接点)
None.gif
function  setParent(node,Pc)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var parentNode = node.getParent();
InBlock.gif 
InBlock.gif    
if(parentNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    
InBlock.gif    
InBlock.gif        
var parentNodeFather = parentNode.getParent();
InBlock.gif        
InBlock.gif        
if(parentNodeFather)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            setParent(parentNode,Pc);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if(Pc)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{parentNode.setAttribute("checked",Pc);}
InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            checkBrother(parentNode,Pc,node.getAttribute(
"NodeData"))
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// 检查子接点是否有选择的,如果有一个选择了,那返回true
None.gif//
只查第一级节点.
None.gif
function  checkBrother(parentNode,Pc,NodeData)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var childNodes = new Array();
InBlock.gif    childNodes 
= parentNode.getChildren();
InBlock.gif    
if(childNodes.length >0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var bChecked = true;
InBlock.gif        
for(var i=0;i<childNodes.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(childNodes[i].getAttribute("checked"== true &&  childNodes[i].getAttribute("NodeData"!= NodeData)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//alert(i+childNodes[i].getAttribute("Text"));
InBlock.gif
                bChecked = false;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
if(bChecked)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            parentNode.setAttribute(
"checked",Pc);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//所有父结点选择
InBlock.gif
            setParent(parentNode,!Pc)
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        parentNode.setAttribute(
"checked",Pc);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// 获取所有节点状态
None.gif
function  FindCheckedFromNode(node)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
var i = 0;
InBlock.gif 
var nodes = new Array();
InBlock.gif nodes 
= node.getChildren();
InBlock.gif 
InBlock.gif 
for (i = 0; i <= nodes.length - 1; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif     
if (nodes[i].getAttribute("Checked"))
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif         AddChecked(nodes[i]);
ExpandedSubBlockEnd.gif     }

InBlock.gif     
if (parseInt(nodes[i].getChildren().length) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif         FindCheckedFromNode(nodes[i]);
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
// 添加选中节点
None.gif
function  AddChecked(node)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    document.all.checked.value 
+= node.getAttribute("NodeData"+ ",";
ExpandedBlockEnd.gif}

None.gif
None.gif
// -->
None.gif
         </ script >
这种方法有个很大的问题,就是他客户端设置的CheckBox在服务器端是不能获取的,现在只能在Check的时候遍历树,并把Checked的值放在一个文本里,然后提交到服务器,然后服务器来解析 1@2@这种字符
            现在我使用的是asp.net2.0,使用的是以下的方法
None.gif function  public_GetParentByTagName(element, tagName) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var parent = element.parentNode;
InBlock.gif    
var upperTagName = tagName.toUpperCase();
InBlock.gif    
//如果这个元素还不是想要的tag就继续上溯
InBlock.gif
    while (parent && (parent.tagName.toUpperCase() != upperTagName)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        parent 
= parent.parentNode ? parent.parentNode : parent.parentElement;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return parent;
ExpandedBlockEnd.gif}

None.gif
None.gif
// 设置节点的父节点Cheched——该节点可访问,则他的父节点也必能访问
None.gif
function  setParentChecked(objNode)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
var objParentDiv = public_GetParentByTagName(objNode,"div");
InBlock.gif    
if(objParentDiv==null || objParentDiv == "undefined")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
var objID = objParentDiv.getAttribute("ID");
InBlock.gif    objID 
= objID.substring(0,objID.indexOf("Nodes"));
InBlock.gif    objID 
= objID+"CheckBox";
InBlock.gif    
var objParentCheckBox = document.getElementById(objID);
InBlock.gif    
if(objParentCheckBox==null || objParentCheckBox == "undefined")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return;
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox")
InBlock.gif    
return
InBlock.gif    objParentCheckBox.checked 
= true;
InBlock.gif    setParentChecked(objParentCheckBox);
ExpandedBlockEnd.gif}

None.gif
None.gif
// 设置节点的子节点uncheched——该节点不可访问,则他的子节点也不能访问
None.gif
function  setChildUnChecked(divID)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
var objchild = divID.children;
InBlock.gif    
var count = objchild.length; 
InBlock.gif    
for(var i=0;i<objchild.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var tempObj = objchild[i];
InBlock.gif        
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tempObj.checked 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        setChildUnChecked(tempObj); 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// 设置节点的子节点cheched——该节点可以访问,则他的子节点也都能访问
None.gif
function  setChildChecked(divID)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
var objchild = divID.children;
InBlock.gif    
var count = objchild.length; 
InBlock.gif    
for(var i=0;i<objchild.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var tempObj = objchild[i];
InBlock.gif        
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tempObj.checked 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        setChildChecked(tempObj); 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// 触发事件
None.gif
function  CheckEvent()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
var objNode = event.srcElement; 
InBlock.gif
InBlock.gif    
if(objNode.tagName!="INPUT" || objNode.type!="checkbox")
InBlock.gif    
return;
InBlock.gif
InBlock.gif    
if(objNode.checked==true)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        setParentChecked(objNode);
InBlock.gif        
var objID = objNode.getAttribute("ID");
InBlock.gif        
var objID = objID.substring(0,objID.indexOf("CheckBox")); 
InBlock.gif        
var objParentDiv = document.getElementById(objID+"Nodes");
InBlock.gif        
if(objParentDiv==null || objParentDiv == "undefined")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        setChildChecked(objParentDiv);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var objID = objNode.getAttribute("ID");
InBlock.gif        
var objID = objID.substring(0,objID.indexOf("CheckBox")); 
InBlock.gif        
var objParentDiv = document.getElementById(objID+"Nodes");
InBlock.gif        
if(objParentDiv==null || objParentDiv == "undefined")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        setChildUnChecked(objParentDiv);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
这种方法最大的好处就是服务端能得javascript设置的Checked的值,不用没点下就全部把树遍利下,直接在服务端遍利一次就行了
None.gif      // 遍历子节点
None.gif
     public   void  GetChildNode(TreeNode Node)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
foreach (TreeNode node in Node.ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (node.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StrChecked 
+= node.Value+"@";
ExpandedSubBlockEnd.gif            }

InBlock.gif            GetChildNode(node);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif}
我照样能得到它的Check的值

转载于:https://www.cnblogs.com/xiongeee/archive/2006/11/20/565640.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值