熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net 2.0 權限樹的控制

2022-06-13   來源: .NET編程 

  做權限的時候主要實現如下功能
    該節點可以訪問則他的父節點也必能訪問
    該節點可以訪問則他的子節點也都能訪問
    該節點不可訪問則他的子節點也不能訪問
    使用帶CheckBox的數型結構能得到很好的用戶體驗可是編程的難度也是有點增加如果全部有服務端來完成那點下刷下肯定不行只能使用javascriptjavascript調試的時候郁悶的很一個字類還有郁悶的遞歸麻煩
    我以前是使用以下該方法

 <script language=javascript>
    <!
            //初始化節點
            initchecknode(documentall(tvItemClientID)valuedocumentall(checked)value)

  //初始化選中節點
                function initchecknode(treetvvalue)
                {
                    //獲得需要初始化選擇狀態的節點的字符串
                    var selectedNodes = tvvalue;
                    var arrayNodes = new Array();
                    arrayNodes = selectedNodessplit();
                    var AllRootNode=new Array();
                    AllRootNode=documentgetElementById(tree)getChildren();
                    //初始化選擇的節點
                    findAndCheckedNodes(AllRootNodearrayNodes);
                }

  //根據已選擇的節點的字符串初始化選擇的節點
                function findAndCheckedNodes(NodeArraynodeDatas)
                {
                    //alert(NodeArraylength);
                     if(parseInt(NodeArraylength)==)
                     {
                        return;
                    }
                    else
                    {
                        for(var i=;i<parseInt(NodeArraylength);i++)
                        {
                            var cNodenodeData;
                            cNode=NodeArray[i];
                            ////如果該節點在nodeDatas裡則初始化checked = true;
                            nodeData = cNodegetAttribute(NodeData);
                            for(var j=;j<nodeDataslength;j++)
                            {
                                if(nodeDatas[j] == nodeData)
                                {
                                    cNodesetAttribute(checkedtrue);
                                    break;
                                }
                            }
                            //如果有子節點則繼續遞歸
                            if(parseInt(cNodegetChildren()length)!=)
                            findAndCheckedNodes(cNodegetChildren()nodeDatas);
                        }
                    }
                }
    //>
    //oncheck事件
    function tree_oncheck(tree)
    {
     var i;
     var node=treegetTreeNode(treeclickedNodeIndex);
     var Pchecked=treegetTreeNode(treeclickedNodeIndex)getAttribute(checked);
     setcheck(nodePchecked);
     setParent(nodePchecked);
    //windowalert(Pchecked);
     documentallcheckedvalue = ;
     if (treegetChildren()length > )
     {
        for (i=;i<=treegetChildren()length;i++)
        {
            if (treegetChildren()[i]getAttribute(Checked))
            {
                 AddChecked(treegetChildren()[i]);
            }
            FindCheckedFromNode(treegetChildren()[i]);
        }
     }
    }
    //設置子節點選中

  function setcheck(nodePchecked)
    {
     var i;
     var ChildNode=new Array();
     ChildNode=nodegetChildren();

  if(parseInt(ChildNodelength)==)
     {
      return;
     }
     else
     {
      for(i=;i<ChildNodelength;i++)
      {
       var cNode;
       cNode=ChildNode[i];
       cNodesetAttribute(CheckedPchecked);
       //cNodeChecked = Pchecked;
       if(parseInt(cNodegetChildren()length)!=)
       {
        setcheck(cNodePchecked);
       }
      }
     }
    }

  //設置子節點選中/取消;
    //同時需要設置父節點的狀態(如果是取消選中的話僅僅設置本節點及其所有字接點不涉及父接點)
    function setParent(nodePc)
    {
        var parentNode = nodegetParent();

  if(parentNode)
        {

  var parentNodeFather = parentNodegetParent();

  if(parentNodeFather)
            {
                setParent(parentNodePc);
            }
            if(Pc)
                {parentNodesetAttribute(checkedPc);}
            else
            {
                checkBrother(parentNodePcnodegetAttribute(NodeData))
            }
        }
    }

  //檢查子接點是否有選擇的如果有一個選擇了那返回true
    //只查第一級節點
    function checkBrother(parentNodePcNodeData)
    {
        var childNodes = new Array();
        childNodes = parentNodegetChildren();
        if(childNodeslength >)
        {
            var bChecked = true;
            for(var i=;i<childNodeslength;i++)
            {
                if(childNodes[i]getAttribute(checked) == true &&  childNodes[i]getAttribute(NodeData) != NodeData)
                {
                    //alert(i+childNodes[i]getAttribute(Text));
                    bChecked = false;
                    break;
                }
            }
            if(bChecked)
            {
                parentNodesetAttribute(checkedPc);
            }
            else
            {
                //所有父結點選擇
                setParent(parentNode!Pc)
            }
        }
        else
        {
            parentNodesetAttribute(checkedPc);
        }
    }

  //獲取所有節點狀態
    function FindCheckedFromNode(node)
    {
     var i = ;
     var nodes = new Array();
     nodes = nodegetChildren();

  for (i = ; i <= nodeslength ; i++)
     {
         if (nodes[i]getAttribute(Checked))
         {
             AddChecked(nodes[i]);
         }
         if (parseInt(nodes[i]getChildren()length) != )
         {
             FindCheckedFromNode(nodes[i]);
         }
     }
    }
    //添加選中節點
    function AddChecked(node)
    {
        documentallcheckedvalue += nodegetAttribute(NodeData) + ;
    }

  //>
            </script>這種方法有個很大的問題就是他客戶端設置的CheckBox在服務器端是不能獲取的現在只能在Check的時候遍歷樹並把Checked的值放在一個文本裡然後提交到服務器然後服務器來解析@@這種字符
                現在我使用的是使用的是以下的方法
    function public_GetParentByTagName(element tagName)
    {
        var parent = elementparentNode;
        var upperTagName = tagNametoUpperCase();
        //如果這個元素還不是想要的tag就繼續上溯
        while (parent && (parenttagNametoUpperCase() != upperTagName))
        {
            parent = parentparentNode ? parentparentNode : parentparentElement;
        }
        return parent;
    }

  //設置節點的父節點Cheched——該節點可訪問則他的父節點也必能訪問
    function setParentChecked(objNode)
    {
        var objParentDiv = public_GetParentByTagName(objNodediv);
        if(objParentDiv==null || objParentDiv == undefined)
        {
            return;
        }
        var objID = objParentDivgetAttribute(ID);
        objID = objIDsubstring(objIDindexOf(Nodes));
        objID = objID+CheckBox;
        var objParentCheckBox = documentgetElementById(objID);
        if(objParentCheckBox==null || objParentCheckBox == undefined)
        {
            return;
        }
        if(objParentCheckBoxtagName!=INPUT && objParentCheckBoxtype == checkbox)
        return;
        objParentCheckBoxchecked = true;
        setParentChecked(objParentCheckBox);
    }

  //設置節點的子節點uncheched——該節點不可訪問則他的子節點也不能訪問
    function setChildUnChecked(divID)
    {
        var objchild = divIDchildren;
        var count = objchildlength;
        for(var i=;i<objchildlength;i++)
        {
            var tempObj = objchild[i];
            if(tempObjtagName==INPUT && tempObjtype == checkbox)
            {
                tempObjchecked = false;
            }
            setChildUnChecked(tempObj);
        }
    }

  //設置節點的子節點cheched——該節點可以訪問則他的子節點也都能訪問
    function setChildChecked(divID)
    {
        var objchild = divIDchildren;
        var count = objchildlength;
        for(var i=;i<objchildlength;i++)
        {
            var tempObj = objchild[i];
            if(tempObjtagName==INPUT && tempObjtype == checkbox)
            {
                tempObjchecked = true;
            }
            setChildChecked(tempObj);
        }
    }

  //觸發事件
    function CheckEvent()
    {

  var objNode = eventsrcElement;

  if(objNodetagName!=INPUT || objNodetype!=checkbox)
        return;

  if(objNodechecked==true)
        {
            setParentChecked(objNode);
            var objID = objNodegetAttribute(ID);
            var objID = objIDsubstring(objIDindexOf(CheckBox));
            var objParentDiv = documentgetElementById(objID+Nodes);
            if(objParentDiv==null || objParentDiv == undefined)
            {
                return;
            }
            setChildChecked(objParentDiv);
        }
        else
        {
            var objID = objNodegetAttribute(ID);
            var objID = objIDsubstring(objIDindexOf(CheckBox));
            var objParentDiv = documentgetElementById(objID+Nodes);
            if(objParentDiv==null || objParentDiv == undefined)
            {
                return;
            }
            setChildUnChecked(objParentDiv);
        }
    }



這種方法最大的好處就是服務端能得javascript設置的Checked的值不用沒點下就全部把樹遍利下直接在服務端遍利一次就行了
        //遍歷子節點

 
        public void GetChildNode(TreeNode Node)
        {
            foreach (TreeNode node in NodeChildNodes)
            {
                if (nodeChecked)
                {
                    StrChecked += nodeValue+@;
                }
                GetChildNode(node);
            }
        }
   我照樣能得到它的Check的值


From:http://tw.wingwit.com/Article/program/net/201311/13498.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.