控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

treeview 的应用

作者:佚名 出处:互联网 2011年03月17日 阅读:

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;
        }
     

        }

    }

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统