tryandtry.cn

导航

 

前言(略)

前台只需要拖动一个TreeView控件

后台代码:

 

protected void Page_Load(object sender, EventArgs e)
    {
        BindCategoryTreeVew(TreeView1);
        TreeView1.ExpandAll();
    }

    //遍历所有的父节点
    public void BindCategoryTreeVew(TreeView treeView)
    {
        DataTable dt = gettrees();//这里可以自己改读取自己的数据库表
        treeView.Nodes.Clear();//清空树的所有节点

        DataRow[] rowList = dt.Select("ParentCategoryID=0");//获取根目录
        foreach (DataRow row in rowList)
        {           
            TreeNode rootNode = new TreeNode();
            rootNode.Text = row["CategoryName"].ToString();
            rootNode.Value = row["CategoryID"].ToString();
            rootNode.NavigateUrl = "";

            rootNode.Target = "right1";
            treeView.Nodes.Add(rootNode);

            //完成一个父节点,紧接着遍历此父节点下的所有子节点,遍历完后再回来执行foreach循环(递归)
            CreateChildNode(rootNode,rootNode.Value,dt);          
        }
       
    }
    //创建子节点
    private void CreateChildNode(TreeNode parentNode,string v,DataTable dt)
    {
        //选择数据,添加排序表达式
        DataRow[] rowList = dt.Select("ParentCategoryID=" + Convert.ToInt32(v));
        foreach (DataRow row in rowList)
        {
            //创建一个新的节点了
            TreeNode node = new TreeNode();
            node.Text = row["CategoryName"].ToString();
            node.Value = row["CategoryID"].ToString();
            node.NavigateUrl = "";
            node.Target = "right1";
        
            //为父根节点添加子节点
            parentNode.ChildNodes.Add(node);

            //递归调用,创建其他节点 (完成一个子节点,遍历此子节点下的子子节点,直到没有子子节点为止;再回来继续刚才没完的循环)
            CreateChildNode(node,node.Value,dt);
        }
    }
    public DataTable gettrees()
    {
        //构造表
        DataTable dt = new DataTable("base_res");
        DataColumn col1 = new DataColumn("CategoryID", System.Type.GetType("System.String"));
        DataColumn col2 = new DataColumn("ParentCategoryID", System.Type.GetType("System.String"));
        DataColumn col3 = new DataColumn("CategoryName", System.Type.GetType("System.String"));
        DataColumn col4 = new DataColumn("CategoryRemark", System.Type.GetType("System.String"));
        DataColumn col5 = new DataColumn("CategoryCode", System.Type.GetType("System.String"));

        dt.Columns.Add(col1);
        dt.Columns.Add(col2);
        dt.Columns.Add(col3);
        dt.Columns.Add(col4);
        dt.Columns.Add(col5);

        System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        string sqlstr = "select * from base_Category where del=0 ";

        System.Data.SqlClient.SqlCommand myCommand1 = new System.Data.SqlClient.SqlCommand(sqlstr, myConnection);
        myConnection.Open();

        System.Data.SqlClient.SqlDataReader dr = myCommand1.ExecuteReader();
        while (dr.Read())
        {
            DataRow rowtemp = dt.NewRow();
            rowtemp[0] = dr.GetValue(0).ToString(); //CategoryID
            rowtemp[1] = dr.GetValue(6).ToString(); //ParentCategoryID
            rowtemp[2] = dr.GetValue(2);  //CategoryName
            dt.Rows.Add(rowtemp);
        }
        dr.Close();
        myConnection.Close();
        return dt;
    }

posted on 2010-11-16 12:01  Lonlong  阅读(697)  评论(0编辑  收藏  举报