treeview 的应用

作者:互联网   出处:控件中国网   2014-11-05 19:05:09   阅读:1

treeview 的应用

前台

    <script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
   <script type="text/javascript">
        function CheckEvent(event)
        {
            evt=window.event||evt;
            var objNode = evt.srcElement||evt.target;
            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);
                            }
                      }
            }
        }

        //判断是否有并行的其他节点被选中
        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;
        }

        //设置父节点
        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){}
        }

        //设置子节点
        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;
             }
           }
        }
      function   client_OnTreeNodeChecked(event)  
        {  
           var   TreeNode   =   event.srcElement   ||   event.target   ;  
           //这是加CheckBox的,改成TreeNode.type   ==   "img "
           if   (TreeNode.tagName   ==   "INPUT "   &&   TreeNode.type   ==   "checkbox ")  
           {  
               if(TreeNode.checked)  
               {            
                   var   checkTitle   =   TreeNode.title;
                   alert(checkTitle);
               }
         }
        }

    </script>  
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" ExpandDepth="1"  PopulateNodesFromClient="true"
         OnTreeNodePopulate="TreeView1_TreeNodePopulate"   ShowLines="true"   ShowExpandCollapse="true"       
          runat="server">
        </asp:TreeView>
         <input id="Hidden1" type="hidden" runat="server"  />
        <asp:Button ID="Button1" runat="server" Text="Button"
            onclick="Button1_Click"  />
    </div>
    </form>
</body>
</html>

后台

  public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateRootLevel();
            TreeView1.Attributes.Add("onclick", "CheckEvent(event)");          
          
        }

       
        private void PopulateRootLevel()
        {
            DataTable dt = SqlHelper.dataTable("SELECT * FROM tb_classtype where fid=0");
            PopulateNodes(dt, TreeView1.Nodes);
        }
        private void PopulateSubLevel(int parentid, TreeNode parentNode)
        {
            DataTable dt = SqlHelper.dataTable("SELECT * FROM tb_classtype where fid=" + parentid + "");
            PopulateNodes(dt, parentNode.ChildNodes);
        }
        protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            PopulateSubLevel(Convert.ToInt32(e.Node.Value), e.Node);
        }
        string strurl = SqlHelper.ExecuteScalar("SELECT  focusdata from  tb_Customer where id=1 ").ToString();
        private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
        {
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode tn = new TreeNode();
                tn.Text = dr["typename"].ToString();
                tn.Value = dr["id"].ToString();


                tn.ShowCheckBox = true;
                nodes.Add(tn);

                tn.PopulateOnDemand = true;
                tn.SelectAction = TreeNodeSelectAction.Expand;

               
                if (strurl.Length <= 0) continue;
                foreach (var item in strurl.Split(','))
                {
                    if (tn.Value == item)
                        tn.Checked = true;
                }

            }
         
        }

        private void CheckBox(TreeNode tnSelect)
        {
            if (TreeView1.CheckedNodes.Count > 0)
            {
                if (tnSelect.Checked)
                {
                    for (int i = 0; i < tnSelect.ChildNodes.Count; i++)
                    {
                        TreeNode tnSelected = tnSelect.ChildNodes[i];
                        tnSelected.Checked = true;
                        CheckBox(tnSelected);
                    }
                }
                else
                {
                    for (int i = 0; i < tnSelect.ChildNodes.Count; i++)
                    {
                        TreeNode tnSelected = tnSelect.ChildNodes[i];
                        tnSelected.Checked = false;
                        CheckBox(tnSelected);
                    }
                }
            }
        }
        protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            CheckBox(e.Node);
        }


        protected void Button1_Click(object sender, EventArgs e)
        {

            string strSelect = TreeChecked(TreeView1.Nodes);


            string strsql = "update tb_Customer set focusdata='" + strSelect + "' where id=1";

            SqlHelper.ExecuteNonQuery(strsql);


        }


        public string chkString = "";

        private string TreeChecked(TreeNodeCollection nodeschk)
        {

            foreach (TreeNode td in nodeschk)
            {

                if (td.Checked)
                {
                    chkString += td.Value + ",";
                }
                if (td.ChildNodes.Count > 0)
                    TreeChecked(td.ChildNodes);
            }
            return chkString;
        }
     

        }

    }

 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat