LINQ绑定TreeView

转帖|其它|编辑:郝浩|2008-09-28 09:49:16.000|阅读 2166 次

概述:LINQ绑定TreeView

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

前台:

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


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:DIY部落

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP