<%@ Page Language="vb" AutoEventWireup="false" 
Codebehind="treeview.aspx.vb" Inherits="appname.treeview"%> 
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls"
Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" %> 
<form id="Form2" method="post" runat="server"> 
     <iewc:treeview id="tvGroups" runat="server" Width="100%" BorderWidth="0px"
Height="100%"></iewc:treeview> 
</form>  So here, a TreeView is created on your page; we will use "tvgroups" to refer it later. 
Now the code behind the page (Treeview.aspx.vb): Imports Microsoft.Web.UI.WebControls 
Imports System.Data.SqlClient 
Public Class Treeview 
    Inherits System.Web.UI.Page 
    Protected WithEvents tvGroups As Microsoft.Web.UI.WebControls.TreeView 
    Dim connString = "your connection string for database" 
    Dim myConnection As New SqlConnection(connString) 
    Dim dtid, dtname As String Here, I'm building the TreeView with the values from the database, so now the remaining code is working with DataSets, DataRows and DataTables. If you know how to use them, it抣l be easy for you; if not, there抯 plenty of documentation about how to use them. 
The following code goes in the Page_Load event/method: If Not Page.IsPostBack Then 
  Dim mysqladapter As New SqlDataAdapter() 
  Dim mydataset As New DataSet() 
  Dim pNode, pChild As TreeNode 
  Dim dttable As DataTable 
  Dim dtrow As DataRow 
  Dim countrow As Integer 
  Dim dtcolumn As DataColumn 
  mysqladapter = New SqlDataAdapter("Fetch_departments", myConnection) 
  mysqladapter.Fill(mydataset, "tbl_no_sales_days") 
  Dim myDataView As DataView = New DataView(mydataset.Tables("tbl_no_sales_days")) 
  myDataView.Sort = "departmentregionid" 
  countrow = myDataView.Count 
  pNode = New TreeNode() 
  pNode.Text = "ALL DEPARTMENTS" 
  pNode.NavigateUrl = "" 
  tvGroups.Nodes.Add(pNode) 
  For Each dttable In mydataset.Tables 
    For Each dtrow In dttable.Rows 
      dtid = dtrow(0) 
      dtname = dtrow(1) 
      pChild = New TreeNode() 
      pChild.NavigateUrl = "http://www.oxx.no" 
      pChild.Text = dtname 
      pChild.ID = dtid 
      pNode.Nodes.Add(pChild) 
    Next 
  Next 
End If In the code above, "Fetch_departments" is a stored procedure that returns regions and departments. 
The output will be something like this: ALL DEPARTMENTS 
--REGION1 
-----DEPT1 
-----DEPT2 
-----DEPT3 
--REGION2 
-----DEPT1 
-----DEPT2 
-----DEPT3 
...TabStrip Control 
Now let抯 look at how to use the TabStrip control. 
The code in the ASPX file is similar to the code for TreeView control: <iewc:tabstrip id="mytabstrip" accessKey="<%# Page %>" runat="server" Width="338px"
Height="28px" AutoPostBack="True" TabSelectedStyle="background-color:#ffffff;color:#000000;"
TabHoverStyle="background-color:#777777;" TabDefaultStyle="background-color:#668274;font-
family:verdana;font-weight:bold;font-size:8pt;color:#ffffff;width:79;height:21;text-align:center;"> 
   <iewc:Tab Text="tab1"></iewc:Tab> 
   <iewc:Tab Text="tab2"></iewc:Tab> 
   <iewc:Tab Text="tab3"></iewc:Tab> 
   <iewc:Tab Text="tab4"></iewc:Tab> 
</iewc:tabstrip>  
The code above will create four tabs in the page. Now it's time to add functionality to these tabs. 
The most common functionality that we have seen in something like this is that a user clicks different tabs, and pages corresponding to the tabs are displayed to the user. I was trying to implement this TabStrip for similar functionality in my project, but I was disappointed to see that there isn't a property or method like Treenode.NavigateUrl. So I created an iframe in the page like this: 
<iframe id="applnname" style="WIDTH: 100.33%; HEIGHT: 175.81%" hspace="0" vspace="0"
src="<%=sIFrameSrc%>" frameBorder="no" height="100%"> 
</iframe> 
This src="" is important, since in the code behind the page, we will populate the frame source so that we can get the required functionality of switching between different pages. 
When you click on different tabs, mytabstrip_onSelectedIndexChange is called, so this is the place where we can change srcframe to different pages. Private Sub mytabstrip_onSelectedIndexChange(ByVal sender As Object, ByVal e As System.EventArgs)
Handles mytabstrip.SelectedIndexChange 
  Dim sdata As String 
  Select Case mytabstrip.SelectedIndex 
    Case 0 
      tabname = "tab1" 
      sIFrameSrc = "http://www.oxx.no" 
    Case 1 
      tabname = "tab2" 
      sIFrameSrc = "http://www.yahoo.com" 
    Case 2 
      tabname = "tab3" 
      sIFrameSrc = "your choice" 
    Case Else 
      sIFrameSrc = "http://www.oxx.no" 
  End Select 
End Sub You should now be able to set up a working TabStrip on your page. The code I have provided is very simple, but these controls can be utilized in many applications. 

解决方案 »

  1.   

    to:lvenlee(大头) thanks
    but i want to add 、delete 、Edit a node ! how to do it in  the sql2000
    how to show a node.text and node.id
      

  2.   

    http://www.ourfly.com/download/downloadlist.aspx?type=Asp.NET
    treeview的演示
      

  3.   

    //树的建立,建树的基本思路是:从根节点开始递归调用显示子树 
    下面的例子经过本人调试成功。 数据库的表结构是cur_id,par_id,f_name.其中:所有的根结点的par_id为-1;而且所有的字段均为VARCHAR2类型。
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码
            If Not IsPostBack Then
               intiTree(TreeView2.Nodes, "-1")
            End If
        End Sub Private Sub intiTree(ByRef Nds As TreeNodeCollection, ByVal parentId As String)
            Dim sql As String = "select * from xj.treeview where par_id='" + parentId + "'"
            Dim myDataAdapter As New OleDbDataAdapter(sql, con)
            Dim ds As New DataSet()
            myDataAdapter.Fill(ds, "tree")
            Dim mydatable As DataTable
            mydatable = ds.Tables("tree")
            If mydatable.Rows.Count = 0 Then
                Exit Sub
            End If
            Dim drv As DataRowView
            Dim tmpNd As TreeNode
            For Each drv In mydatable.DefaultView
                tmpNd = New TreeNode()
                Dim strid As String
                strid = drv("CUR_ID")
                tmpNd.ID = strid
                tmpNd.Text = drv("F_NAME")
                Nds.Add(tmpNd)
                intiTree(tmpNd.Nodes, strid)
            Next
        End Sub
    增加、删除树节点:单纯在Treeview 上增加、删除、修改节点只需用Nodes属性的Add、 Remove、等方法即可,VS.NET中的是分层的每个Node下都有Nodes属性。增加、删除、修改树节点时与VS6.0相比有很大差别,特别是删除时。 
    Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click’在选定的节点下添加子节点 
    Dim tmpNd As New TreeNode(), NdSel As TreeNode 
    tmpNd.ID = GetNewId() 
    NdSel = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)’选中的节点 
    tmpNd.Text = "新节点" 
    NdSel.Nodes.Add(tmpNd) 
    Dim myRow As DataRow 
    myRow = ds.Tables("tree").NewRow() 
    myRow("NODE_NAME") = tmpNd.ID 
    myRow("NODE_DESCRIPT") = "新节点" & tmpNd.ID & "_" & NdSel.ID 
    myRow("PARENT_NAME") = NdSel.ID 
    ds.Tables("tree").Rows.Add(myRow) 
    End Sub Private Sub ButDele_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButDele.Click’删除选中的节点 
    Dim idx As String = TreeView1.SelectedNodeIndex() 
    GetNdCol(idx).Remove(TreeView1.GetNodeFromIndex(idx)) 
    Dim dv As New DataView(), recNo As Integer 
    dv.Table = ds.Tables("tree") 
    dv.RowFilter= "NODEID=" & NdId 
    dv.Delete(0) 
    End Sub 
    Private Function GetNdCol(ByVal idx As String) As TreeNodeCollection 
    ‘获得选中节点的父节点的Nodes集合 
    Dim cnt As Integer, i As Integer 
    Dim tmpNds As TreeNodeCollection 
    Dim idxs() As String 
    idxs = Split(idx, ".") 
    cnt = UBound(idxs) 
    If cnt = 0 Then 
    tmpNds = TreeView1.Nodes 
    Else 
    tmpNds = TreeView1.Nodes(CInt(idxs(0))).Nodes 
    For i = 1 To cnt - 1 
    tmpNds = tmpNds(CInt(idxs(i))).Nodes 
    Next 
    End If 
    Return tmpNds 
    End Function