IE Web Controls in VB.NET - TreeView Control
(Page 2 of 4 )
Once you've downloaded and installed IE Web Controls, create a new ASP.NET application and make sure the TreeView control is in the toolbox.
Add the treeview control to your aspx form and in HTML view it will look something like this:
<%@ 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 and we will use "tvgroups" to refer it later.
Now the code behind 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 am building the tree view 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’ll be easy for you, if not then there’s 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 which returns regions and departments.
The output will be something like this:
ALL DEPARTMENTS
--REGION1
-----DEPT1
-----DEPT2
-----DEPT3
--REGION2
-----DEPT1
-----DEPT2
-----DEPT3
...Next: TabStrip Control >>
More ASP.NET Articles
More By Himanshu Dhami