LINQ绑定TreeView

来源:互联网 发布:java嘉华垃圾桶 编辑:程序博客网 时间:2024/05/31 13:16

前台:

<table cellpadding="5" cellspacing="0" style="border:1px; width:100%;" >
                <tr >
                    <td style="width:70%;">
    <asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0"
        onselectednodechanged="TreeView1_SelectedNodeChanged">
    </asp:TreeView>
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <asp:Label ID="Label2" runat="server"></asp:Label>
    </td>

    </tr>
    </table>

后台:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        //绑定图书分类树
        protected void BindData()
        {
            BookClassificationBusiness bcb = new BookClassificationBusiness();
            var aa = bcb.GetBookClassification();
            if (aa.Any())
            {
                foreach (var a in aa)
                {
                    TreeNode node = new TreeNode();
                    node.Text = a.ClassificationName;
                    node.Value = Convert.ToString(a.Childid);
                    TreeView1.Nodes.Add(node);
                    BindChildData(node, a.Childid);
                }
            }
        }
        //绑定子节点
        protected void BindChildData(TreeNode tn,int parentid)
        {
            BookClassificationBusiness bcb = new BookClassificationBusiness();
            var bb = bcb.GetChildBookClass(parentid);
            if (bb.Any())
            {
                foreach (var b in bb)
                {
                    TreeNode Cnode = new TreeNode();
                    Cnode.Text =b.ClassificationName;
                    Cnode.Value = Convert.ToString(b.Childid);
                    tn.ChildNodes.Add(Cnode);
                    BindChildData(Cnode, b.Childid);
                }
            }
        }
        //取得父节点文字
        protected void GetParentName(TreeNode tn)
        {
            if (tn.Parent != null)
            {
                Label1.Text += "-" + tn.Parent.Text;
                Label2.Text += "," + tn.Parent.Value;
                GetParentName(tn.Parent);
            }
        }
        //反转字符串
        protected string TurnString(string str,char c)
        {
            string[] aa = str.Split(c);
            string bb = "";
            for (int i = aa.Length - 1; i > 0; i--)
            {
                bb += aa[i] + c;
            }
            bb += aa[0];
            return bb;
        }
        //得到图书分类字符串

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            Label1.Text = TreeView1.SelectedNode.Text;
            Label2.Text = TreeView1.SelectedNode.Value;
            GetParentName(TreeView1.SelectedNode);
            Label1.Text = TurnString(Label1.Text, '-');
            Label2.Text = TurnString(Label2.Text, ',');
        }

数据库设计:

ChildId    ParentID ClassName

 

业务逻辑层:

        public IQueryable<COM_BookClassification> GetBookClassification()
        {
            CCPressDataContext cp = new CCPressDataContext();
            var aa = from a in cp.COM_BookClassification
                     where a.Parentid==0
                     select a;
            return aa;
        }

说明:根目录的id固定为0;