DataTable table1;
    TreeNode trNewNode;
    WebService webService = new WebService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strSql = "Select * From class;";
            table1 = webService.ExcuteSelect(strSql);//获得所有的类型的数据表
            foreach (DataRow drRow in table1.Rows)//对数据表中的每一行
            {
                if (drRow["upid"].ToString() == "0")//当它为最高一级
                {
                    trNewNode = new TreeNode(drRow["classid"].ToString(), drRow["id"].ToString());//新建节点
                    trNewNode.SelectAction = TreeNodeSelectAction.Select;//设置节点的选择动作
                    this.treeSort.Nodes.Add(trNewNode);//添加节点
                    if (drRow["pd"].ToString() == "1")//判断是否有字节点
                    {
                        DataTable childTable = table1.Clone();//如果有,则新建一个同样结构的数据表
                        foreach (DataRow row in table1.Rows)//得到它的字节点列表
                        {
                            if (row["upid"].ToString() == drRow["id"].ToString())
                            {
                                DataRow newRow = childTable.NewRow();
                                newRow.ItemArray = row.ItemArray;
                                childTable.Rows.Add(newRow);
                            }
                        }
                        Make_ChildNode(trNewNode, childTable);//递归
                    }
                }
            }
        }
    }
    protected void Make_ChildNode(TreeNode node, DataTable dtTable)
    {
        foreach (DataRow drRow in dtTable.Rows)//对字节点数据表
        {
            trNewNode = new TreeNode(drRow["classid"].ToString(), drRow["id"].ToString());//新建节点
            trNewNode.SelectAction = TreeNodeSelectAction.Select;//设置节点的选择动作
            node.ChildNodes.Add(trNewNode);//添加节点
            if (drRow["pd"].ToString() == "1")//判断是否有字节点
            {
                DataTable childTable = table1.Clone();//如果有,则新建一个同样结构的数据表
                foreach (DataRow row in table1.Rows)//得到它的字节点列表
                {
                    if (row["upid"].ToString() == drRow["id"].ToString())
                    {
                        DataRow newRow = childTable.NewRow();
                        newRow.ItemArray = row.ItemArray;
                        childTable.Rows.Add(newRow);
                    }
                }
                Make_ChildNode(trNewNode, childTable);//继续递归
            }
        }
    }
    protected void treeSort_SelectedNodeChanged(object sender, EventArgs e)
    {
        string strTypeID = this.treeSort.SelectedNode.Value;
        if (Session["UserLevel"] != null && Session["UserLevel"].ToString() == "Admin")
        {
            Response.Redirect("Goods_List.aspx?type=" + strTypeID);
        }
        else
        {
            Response.Redirect("User_GoodsList.aspx?type=" + strTypeID);
        }
    }
}

解决方案 »

  1.   

    <asp:TreeView ID="treeSort" runat="server" Height="218px" ImageSet="Arrows" OnSelectedNodeChanged="treeSort_SelectedNodeChanged"
                    Width="146px">
                    <ParentNodeStyle Font-Bold="False" />
                    <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
                    <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px"
                        VerticalPadding="0px" />
                    <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
                        NodeSpacing="0px" VerticalPadding="0px" />
                    <LeafNodeStyle NodeSpacing="0px" VerticalPadding="0px" />
                </asp:TreeView>
      

  2.   

    估计是想分享的 不过对于树结构很复杂的  递归会严重的影响 处理效率  因为是一次加载的
    建议LZ研究下 treeview的 TreeNodePopulate