asp.net TreeView_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > asp.net TreeView

asp.net TreeView

 2015/4/6 16:48:05  嘿嘿v8v  程序员俱乐部  我要评论(0)
  • 摘要:希望有实力的朋友了以后能指点我改一下选中和不选中的递归前台代码<divstyle="font-size:14px;"><asp:TreeViewShowCheckBoxes="All"ID="usertree"runat="server"OnTreeNodeCheckChanged="MyTreeView_TreeNodeCheckChanged"OnSelectedNodeChanged="MyTreeView_SelectedNodeChanged"><
  • 标签:.net ASP.NET view net

希望有实力的朋友了以后能指点我改一下选中和不选中的递归

 

前台代码

class="code_img_closed" src="/Upload/Images/2015040616/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('bd0b63b3-df47-42c3-a297-30185e1c0273',event)" src="/Upload/Images/2015040616/2B1B950FA3DF188F.gif" alt="" />
  <div style="font-size:14px;">
                        <asp:TreeView ShowCheckBoxes="All" ID="usertree" runat="server" OnTreeNodeCheckChanged="MyTreeView_TreeNodeCheckChanged" OnSelectedNodeChanged="MyTreeView_SelectedNodeChanged" >
                        </asp:TreeView>
                    </div>
    
    
    
               <div style="float:left;">
           <span style="display:block">要参加人员:</span>
            <asp:ListBox ID="NeedExamStaff" AutoPostBack="true" runat="server" Height="262px" Width="116px" SelectionMode="Multiple">
            <asp:ListItem Value="张三"></asp:ListItem>
            <asp:ListItem Value="李四"></asp:ListItem>
            <asp:ListItem>王五</asp:ListItem>
            </asp:ListBox>
                <hr />
             <span style="margin-bottom:0px">总 人 员:<asp:TextBox ID="NeedStaffNumber" runat="server" Text="" Enabled="false" Height="16px" Width="56px"></asp:TextBox><br /><span style="color:red;font-size:small" id="VNeedStaffNumber" runat="server"></span></span>

             </div>  
             <asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" Height="21px" />
View Code

 

后台代码

       DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "User ID=sa;Initial Catalog=newtest;Data Source=JJ-PC;Password=123";

                // 打开连接
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                var sql = "select * from city";

                // 初始化命令
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;

                // 用于执行数据插入、更新和删除的操作;返回被影响的行数。
                int i = cmd.ExecuteNonQuery();



                // 如果想获取数据集合的话我们经常使用到的是数据适配器

                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                adapter.Fill(dt);
                this.usertree.Nodes.Clear();
                usertree.Attributes.Add("onclick", " postBackByObject()");

                AddTree(110000, (TreeNode)null);
            }
        }



        //递归添加树的节点
        public void AddTree(int ParentID, TreeNode pNode)
        {
            DataTable ds = dt;
            DataView dvTree = new DataView(ds);
            //过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter = "[fatherID] = " + ParentID;

            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                if (pNode == null)
                {
                    //添加根节点
                    Node.Text = Row[2].ToString();
                    Node.Value = Row[1].ToString();
                    this.usertree.Nodes.Add(Node);
                    Node.Expanded = true;
                    Node.ShowCheckBox = true;
                    //再次递归
                    AddTree(Int32.Parse(Row[1].ToString()), Node);
                }
                else
                {
                    //添加当前节点的子节点
                    Node.Text = Row[2].ToString();
                    Node.Value = Row[1].ToString();
                    pNode.ChildNodes.Add(Node);
                    Node.Expanded = true;
                    Node.ShowCheckBox = true;
                    //再次递归
                    AddTree(Int32.Parse(Row[1].ToString()), Node);
                }
            }
        }

        protected void MyTreeView_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            TreeNode node = e.Node;
            var nodeCount = node.ChildNodes.Count;
            if (node.Checked == true)
            {
                if (nodeCount > 0)
                {
                    for (var i = 0; i < nodeCount; i++)
                    {
                        node.ChildNodes[i].Checked = true;
                        var node1Count = node.ChildNodes[i].ChildNodes.Count;
                        if (node1Count > 0)
                        {
                            for (var k = 0; k < node1Count; k++)
                            {
                                node.ChildNodes[i].ChildNodes[k].Checked = true;
                                var node2Count = node.ChildNodes[i].ChildNodes[k].ChildNodes.Count;
                                if (node2Count > 0)
                                {
                                    for (var j = 0; j < node2Count; j++)
                                    {
                                        node.ChildNodes[i].ChildNodes[k].ChildNodes[j].Checked = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (nodeCount > 0)
                {
                    for (var i = 0; i < nodeCount; i++)
                    {
                        node.ChildNodes[i].Checked = false;
                        var node1Count = node.ChildNodes[i].ChildNodes.Count;
                        if (node1Count > 0)
                        {
                            for (var k = 0; k < node1Count; k++)
                            {
                                node.ChildNodes[i].ChildNodes[k].Checked = false;
                                var node2Count = node.ChildNodes[i].ChildNodes[k].ChildNodes.Count;
                                if (node2Count > 0)
                                {
                                    for (var j = 0; j < node2Count; j++)
                                    {
                                        node.ChildNodes[i].ChildNodes[k].ChildNodes[j].Checked = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void MyTreeView_SelectedNodeChanged(object sender, EventArgs e)
        {
            var tt = usertree.SelectedNode.Checked;
            var dd = usertree.SelectedValue;
            databind();
        }

        public void databind()
        {

            if (usertree.SelectedNode.Checked == false)
            {
                if (usertree.SelectedNode.ChildNodes.Count > 0)
                {
                    foreach (TreeNode subNode in usertree.SelectedNode.ChildNodes)
                    {
                        usertree.SelectedNode.Checked = true;
                        subNode.Checked = true;
                        if (subNode.ChildNodes.Count > 0)
                        {
                            foreach (TreeNode subNode2 in subNode.ChildNodes)
                            {
                                subNode2.Checked = true;

                                if (subNode2.ChildNodes.Count > 0)
                                {
                                    foreach (TreeNode subNode3 in subNode2.ChildNodes)
                                    {
                                        subNode3.Checked = true;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    usertree.SelectedNode.Checked = true;
                }
            }
            else
            {
                if (usertree.SelectedNode.ChildNodes.Count > 0)
                {
                    foreach (TreeNode subNode in usertree.SelectedNode.ChildNodes)
                    {
                        usertree.SelectedNode.Checked = false;
                        subNode.Checked = false;
                        if (subNode.ChildNodes.Count > 0)
                        {
                            foreach (TreeNode subNode2 in subNode.ChildNodes)
                            {
                                subNode2.Checked = false;

                                if (subNode2.ChildNodes.Count > 0)
                                {
                                    foreach (TreeNode subNode3 in subNode2.ChildNodes)
                                    {
                                        subNode3.Checked = false;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    usertree.SelectedNode.Checked = false;
                }
            }
            usertree.SelectedNode.Selected = false;
        }




        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (TreeNode tn in usertree.CheckedNodes)
            {
                try
                {

                    string userid = tn.Value.ToString();
                  
                        ListItem li = new ListItem();
                       li.Text = tn.Value;
                       li.Value = tn.Text;

                        var tt = 0;
                        for (var i = 0; i < NeedExamStaff.Items.Count; i++)
                        {

                            if (NeedExamStaff.Items[i].Value == li.Value)
                            {
                                tt = 1;
                            }
                        }

                        if (tt == 0)
                        {
                            this.NeedExamStaff.Items.Add(li);

                            if (this.NeedStaffNumber.Text != "")
                            {
                                this.NeedStaffNumber.Text = (Convert.ToInt32(this.NeedStaffNumber.Text) + 1).ToString();
                                this.VNeedStaffNumber.InnerText = "";
                            }
                            else
                            {
                                this.NeedStaffNumber.Text = "1";
                                this.VNeedStaffNumber.InnerText = "";
                            }

                        }

                    }
                
                catch
                {

                }
            }
        }
View Code

 

上一篇: Mvc学习笔记(4) 下一篇: 没有下一篇了!
发表评论
用户名: 匿名