asp.net手动填充TreeView生成树

  最近在做项目发现需要用到树的地方,页面的前台任然是使用一个asp.net的控件TreeView来显示树的结构,当然也可以自己在前台写一个树来展示,这在后期跟局功能的不同很大可能会要用到异步的知识,废话不多说,贴代码:

  首先前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="txl.aspx.cs" Inherits="web.txl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Styles/basic.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>   
</head>
<body>
    <form id="form1" runat="server">
 <asp:TreeView ID="tvw" runat="server" CssClass="">
            </asp:TreeView>
</form>
</body>    

后台代码:

        /// <summary>
        /// 绑定TreeView节点
        /// </summary>
        private void BindTreeView()
        {      
       //从数据读取出父节点的数据
var fzmodel = new BLL_Wdgl_txlfz().GetModelList(" txlid=" + txlmodel.id.ToString());
       foreach (var item in fzmodel) { TreeNode parentNode = new TreeNode(); parentNode.Text = item.fzmc.ToString(); parentNode = BindParent(item.id, parentNode); tvw.Nodes.Add(parentNode); } } /// <summary> /// 给TreeView绑定子节点 /// </summary> /// <param name="fzid">分组id</param> /// <param name="parentNode">父节点</param> /// <returns>返回整个父节点</returns> private TreeNode BindParent(long fzid, TreeNode parentNode) {
       //在数据库中根据传递过来的父节点的ID找出子节点的集合填充到parentNode中
var cymodel = new BLL_Wdgl_txlcy().GetModelList(" fzid=" + fzid); foreach (var item in cymodel) { TreeNode childnode = new TreeNode(); childnode.Text = item.mc.Trim(); parentNode.ChildNodes.Add(childnode); } return parentNode; }

 

posted @ 2013-11-10 10:12  .Justme  阅读(362)  评论(0编辑  收藏  举报