代码改变世界

ASP.NET树形1

2009-04-21 20:13  观海看云  阅读(105)  评论(0编辑  收藏  举报
/// <summary>
    /// 树行绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void treeviewProductDataBind()
    {
        //根节点
        DataTable dtCategories = objDs.GetData();
        int categoriesLen = dtCategories.Rows.Count;
        //子节点
        DataTable dtProductsCount = objDs.GetDataBy();
        int productsLen = dtProductsCount.Rows.Count;
        //添加根节点
        for (int i = 0; i < categoriesLen; i++)
        {
            TreeNode treenodeParent = new TreeNode();
            treenodeParent.Text = dtCategories.Rows["departmentname"].ToString();
            treenodeParent.Value = dtCategories.Rows["departmentid"].ToString();
            treenodeParent.NavigateUrl = "Departmen.aspx?FONT-STYLE: italic">["departmentid"].ToString() == dtProductsCount.Rows[j]["parentid"].ToString())
                {
                    TreeNode treenodeChild = new TreeNode();
                    treenodeChild.Text = dtProductsCount.Rows[j]["departmentname"].ToString();
                    treenodeChild.Value = dtProductsCount.Rows[j]["departmentid"].ToString();
                    treenodeChild.NavigateUrl = "Paren.aspx?departmentid=" + treenodeParent.Value.ToString() + "&parentid=" + treenodeChild.Value.ToString();
                    treenodeParent.ChildNodes.Add(treenodeChild);
                }
            }
        }
        //TreeNode treenodeParent2 = new TreeNode();
        //treenodeParent2.ImageUrl = "~/images/A1.gif";
        //treenodeParent2.NavigateUrl = "BuMen.aspx";
        //treenodeParent2.Text = "部门管理";
        //this.TreeView1.Nodes.Add(treenodeParent2);
    }
    /// <summary>
    /// 节点展开
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        foreach (TreeNode treenode in this.TreeView1.Nodes)
        {
            if (treenode.Value != e.Node.Value)
            {
                treenode.CollapseAll();
            }
        }
    }