asp.net web application (in VB)treeview控件怎么通过代码和数据库绑定?可不可以不通过xml文件?直接在.aspx里写函数来建树?

解决方案 »

  1.   

    动态增加节点,很容易的。
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection myConnection;
                SqlCommand myCommand;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
                myCommand = new SqlCommand();
                myCommand.Connection = myConnection;
                myCommand.CommandType = CommandType.Text;
                myCommand.CommandText = "SELECT [SoftFirstClassID], [SoftFirstClassName] FROM [SoftFirstClass]";
                
                myConnection.Open();
                SqlDataReader sr = myCommand.ExecuteReader();
                try
                {
                    while (sr.Read())
                    {
                        TreeNode tn1 = new TreeNode();
                        
                        tn1.Text = sr.GetString(1);
                        tn1.Selected = false;
                        tn1.SelectAction = TreeNodeSelectAction.None;
                        
                        TreeView1.Nodes.Add(tn1);                    int firstClassID = sr.GetInt32(0);
                        SqlConnection secondConnection;
                        SqlCommand secondCommand;
                        secondConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
                        secondCommand = new SqlCommand();
                        secondCommand.Connection = secondConnection;
                        secondCommand.CommandType = CommandType.Text;
                        secondCommand.CommandText = "SELECT [SoftSecondClassID], [SoftSecondClassName] FROM [SoftSecondClass] WHERE ([SoftFirstClassID] = @SoftFirstClassID)";
                
                        secondCommand.Parameters.AddWithValue("@SoftFirstClassID", firstClassID);
                        secondConnection.Open();
                        SqlDataReader sr2 = secondCommand.ExecuteReader();
                        try
                        {
                            
                            while (sr2.Read())
                            {
                                TreeNode tn2 = new TreeNode();
                                tn2.Text = sr2.GetString(1);
                                tn2.Value = (sr2.GetInt32(0)).ToString();
                                
                                //TreeView1.Nodes.AddAt(i, tn2);
                                tn1.ChildNodes.Add(tn2);
                            }
                        }
                        finally 
                        {
                            sr2.Close();
                            secondConnection.Close();
                        }
                    }
                }
                finally
                {
                    sr.Close();
                    myConnection.Close();
                }
            }    }
    两级树代码
      

  2.   

    树:
    1.http://www.meizz.com/Web/Web.asp2.
    微软网站
    http://www.microsoft.com/china/msdn/archives/library/workshop/webcontrols/overview/treeview.asp3.
    引用:
    http://community.csdn.net/Expert/topic/4196/4196863.xml?temp=9.365261E-03
    http://community.csdn.net/Expert/topic/3169/3169028.xml?temp=.8414118
      

  3.   

    谢谢楼上两位!
    我找了一个用递归方法写的动态生成树的函数(in vb)
    不过因为对DataRowView一些属性不大清楚,有的地方不知要怎么相应地改动
    请高手帮忙看看
     Public Sub AddTree(ByVal ParentID As Integer, ByVal pNode As TreeNode)
            Dim DataView_Tree As DataView
            DataSet_Tree = Me.ViewState("DataSet_Tree")
            DataView_Tree = New DataView(DataSet_Tree.Tables(0))
            DataView_Tree.RowFilter = "*******" '.ToString() & (ParentID.ToString())
            Dim Row As DataRowView        For Each Row In DataView_Tree
                Dim node = New TreeNode
                node.NavigateUrl = "List.aspx?Item_ID=" + Row("****").ToString()
                Dim aa As New TreeNode
                If ParentID = 0 Then                '添加根节点
                    node.Text = Row("*****").ToString()
                    TreeView_Tree.Nodes.Add(node)
                    node.Expanded = True
                    AddTree(Int32.Parse(Row(***)), node)            Else
                    node.Text = Row("****").ToString()
                    pNode.Nodes.Add(node)
                    node.Expanded = True
                    AddTree(Int32.Parse(Row("****").ToString()), node)            End If        Next    End Sub
    数据库中有表topnode, midnode, lownode
    topnode 中有字段:topid, toptext
    midnode: midid, topid, midtext
    lownode: lowid, midid, lowtext结果是要实现从数据库中读取后分层显示,toptext,midtext,lowtext的内容分别在第一、二、三层