asp 使用TreeView控件

这段代码为了使用 TreeNodeCheckChanged 事件,会有回刷新的效果;

不喜欢的可查看改进版,利用js控制选择操作,无界面刷新, “http://www.cnblogs.com/GoCircle/p/6231985.html”

前台代码

    <style>
        .tn td {
            height: 18px;
            display: flex;
        }

        .tn tr {
            display: flex;
        }

        .tn a {
            line-height: 18px;
        }
    </style>
    <script type="text/javascript">
        function postBackByObject() {
            var o = window.event.srcElement;
            if (o.type === "checkbox") {
                //第一个参数写up_action的ID,否则就是整个页面刷了
                __doPostBack("up_action", "");
            }
        }
    </script>

                        <asp:UpdatePanel ID="up_action" runat="server">
                            <ContentTemplate>
                                <asp:TreeView ID="tv_roleaction" runat="server" ShowCheckBoxes="All" CssClass="tn" OnTreeNodeCheckChanged="tv_roleaction_TreeNodeCheckChanged" ShowLines="True" CollapseImageToolTip="折叠">
                                </asp:TreeView>
                            </ContentTemplate>
                        </asp:UpdatePanel>

后台

注:由于页面加载问题,请在关闭或保存页面信息后重新new一下两个私有变量

        private static SysRoleEntity role = new SysRoleEntity();

        private static List<object> roleaction = new List<object>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //调用递归函数,完成树形结构的生成
                AddTree(0, (TreeNode)null);
                tv_roleaction.Attributes.Add("onclick", "postBackByObject()");
            }
        }


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

            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                Node.Value = Row["FACTIONID"].ToString();
                Node.Expanded = true;
                if (pNode == null)
                {    //添加根节点
                    Node.Text = "<font style='color:gray'>" + Row["FACTIONNAME"].ToString() + "</font>";
                    tv_roleaction.Nodes.Add(Node);
                }
                else
                {   //̀添加当前节点的子节点
                    Node.Text = "<font style='color:gray'>" + Row["FACTIONNAME"].ToString() + "</font>";
                    pNode.ChildNodes.Add(Node);
                }

                //判断当前角色是否拥有该权限
                if (roleaction.Count > 0 && roleaction.Find(p => p.ToString() == Row["FACTIONID"].ToString()) != null)
                {
                    Node.Checked = true;
                    Node.Text = Regex.Replace(Node.Text, @"gray", @"green");
                }
                AddTree(Int32.Parse(Row["FACTIONID"].ToString()), Node);     //再次递归
            }
        }

        /// <summary>
        /// 节点的选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void tv_roleaction_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            //设置该节点的属性   变量roleaction的修改
            if (e.Node.Checked)
            {
                roleaction.Add(e.Node.Value);
                e.Node.Text = Regex.Replace(e.Node.Text, @"gray", @"green");
            }
            else
            {
                roleaction.Remove(e.Node.Value);
                e.Node.Text = Regex.Replace(e.Node.Text, @"green", @"gray");
            }

            SetChildChecked(e.Node);
            SetParentChecked(e.Node);
        }

        //设置该结点的子节点
        private void SetChildChecked(TreeNode parentNode)
        {
            foreach (TreeNode node in parentNode.ChildNodes)
            {
                //子节点是否选并改变颜色
                node.Checked = parentNode.Checked;
                if (node.Checked)
                {
                    roleaction.Add(node.Value);
                    node.Text = Regex.Replace(node.Text, @"gray", @"green");
                }
                else
                {
                    roleaction.Remove(node.Value);
                    node.Text = Regex.Replace(node.Text, @"green", @"gray");
                }

                if (node.ChildNodes.Count > 0)
                {
                    SetChildChecked(node);
                }
            }
        }

        //设置该结点的父节点
        private void SetParentChecked(TreeNode childNode)
        {
            if (childNode.Parent != null)
            {
                var ifnochecked = true;
                //检查父节点下是否有选中的节点
                foreach (TreeNode node in childNode.Parent.ChildNodes)
                {
                    if (node.Checked)
                    {
                        if (roleaction.Where(p => p.ToString().Equals(childNode.Parent.Value)).Count() == 0)
                        {
                            roleaction.Add(childNode.Parent.Value);
                        }
                        ifnochecked = false;
                        childNode.Parent.Checked = true;
                        childNode.Parent.Text = Regex.Replace(childNode.Parent.Text, @"gray", @"green");
                        break;
                    }
                }
                if (ifnochecked)
                {
                    roleaction.Remove(childNode.Parent.Value);
                    childNode.Parent.Checked = false;
                    childNode.Parent.Text = Regex.Replace(childNode.Parent.Text, @"green", @"gray");
                }
                SetParentChecked(childNode.Parent);
            }
        }
有错误的请多多指教,共同进步(๑•ᴗ•๑)
By听雨的人
原文地址:https://www.cnblogs.com/GoCircle/p/6203095.html