关闭

asp.net中动态生成 Microsoft.Web.UI.WebControls.TreeView

1147人阅读 评论(0) 收藏 举报

撑着油纸伞, 独自彷徨在悠长, 悠长又寂寥的雨巷;

  

 改改数据库,

create view miaoCC
as
SELECT fid AS ID, sname AS Name, pid AS PID
FROM fsort
UNION
SELECT fid AS ID, sname AS Name, sid AS PID
FROM tsort
UNION
SELECT sid AS ID, sname AS Name, fid AS PID
FROM ssort

 

 private  void AddTree(string id,TreeNode pNode)
  {
   SqlConnection myConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DB"]);

   DataSet ds=Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(myConnection,System.Data.CommandType.Text,"select * from miaoCC where PID= '"+id +"'");
   DataView dv=new DataView(ds.Tables[0]);
   
   foreach(DataRowView row in dv)
   {
    TreeNode node=new TreeNode();
    if(pNode==null)
    {
     node.Text=row["Name"].ToString();
     TreeView1.Nodes[0].Nodes.Add(node);
     node.ImageUrl="images/home.gif";
     node.ExpandedImageUrl="images/btn2.gif";
     node.Expanded=false;    
     AddTree(row["ID"].ToString(),node);
    }
    else
    {
        node.Text=row["Name"].ToString();
     node.ID=row["ID"].ToString();
     pNode.Nodes.Add(node);     
     node.Expanded=false;
     node.ImageUrl="images/home.gif";
     node.ExpandedImageUrl="images/btn2.gif";
     AddTree(row["ID"].ToString(),node);
    }
    
   }
  }
 

把3个表用union合并在一起,即实现了经典的迭代算法,;

即在查询中生成了,必须的三列;

ID    Content      ParentID

AddTree("0", (TreeNode)null); 

,然后再调用;

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:164869次
    • 积分:2680
    • 等级:
    • 排名:第13563名
    • 原创:81篇
    • 转载:39篇
    • 译文:2篇
    • 评论:106条
    最新评论