天道酬勤

博观而约取,厚积而薄发!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

TreeView读取数据库结点【转】

Posted on 2010-04-29 17:05  Happy Coding  阅读(324)  评论(0编辑  收藏  举报

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using clsTreeViewName;

public partial class TreeNode读取数据库_TreeContral : System.Web.UI.UserControl
{
    DataView dv = new DataView();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTreeView();
            TreeView1.ExpandDepth = 0;
            Response.Write("fffffffff");
            TreeView1.CollapseAll();
        }
    }
    void BindTreeView()
    {
        string sql = "select * from DropDownList";
        clsTreeViewName.clsTreeView ctv = new clsTreeView();
        dv = ctv.selectDs(sql).Tables[0].DefaultView;

        foreach (DataRowView i in dv)
        {
            if (i["fatherId"].ToString() == "0")
            {
                TreeNode tn = new TreeNode();
                tn.Text = i["stateName"].ToString();
                tn.Value = i["stateId"].ToString();
                tn.NavigateUrl = i["url"].ToString();
                TreeView1.Nodes.Add(tn);
               
                CreateChildNode(tn);            
            }
        }
    }
    void CreateChildNode(TreeNode t)
    {
        foreach (DataRowView i in dv)
        {
            if (i["fatherId"].ToString() == t.Value.ToString())
            {
                TreeNode tn2 = new TreeNode();
                tn2.Text = i["stateName"].ToString();
                tn2.Value = i["stateId"].ToString();
                tn2.NavigateUrl = i["url"].ToString();
                t.ChildNodes.Add(tn2);
                CreateChildNode(tn2);
                TreeView1.ShowLines = true;              
             }
        }
    }