Microsoft KB Archive/319441

= How To Dynamically Add Nodes to a TreeView WebBrowser Control by Using Visual Basic .NET =

Article ID: 319441

Article Last Modified on 4/21/2006

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft ASP.NET 1.1
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q319441



For a Microsoft Visual C# .NET version of this article, see 320909.

IN THIS TASK
SUMMARY Download Internet Explorer WebBrowser Controls Add the TreeView Control to the Visual Studio .NET Toolbox Create the Visual Basic .NET Project and Add the TreeView Control to the Form Add Code to Send Data
 * Send All Data Immediately
 * Send Only Root Nodes and Requested Child Nodes
 * Send Only Root Nodes and Minimum Child Nodes

REFERENCES



SUMMARY
This step-by-step article demonstrates how to add multiple levels of nodes to a Microsoft Internet Explorer TreeView WebBrowser control by using a DataSet object that includes multiple tables.

This article also demonstrates three ways to optimize how much data you send and when you send the data to the client. For a small set of data, you want to send all of the data immediately. For a larger set of data, you may want to send only the root level nodes and then add only child nodes that are requested. For even larger sets of data, you may want to remove child nodes that are no longer needed.

back to the top

Download Internet Explorer WebBrowser Controls
You must download the Internet Explorer WebBrowser controls to your development computer before you can use the TreeView WebBrowser control in a Web application. To download the WebBrowser controls, visit the following Microsoft Web site:

http://www.asp.net/ControlGallery/default.aspx?Category=38&tabindex=2

back to the top

Add the TreeView Control to the Visual Studio .NET Toolbox
To add the TreeView WebBrowser control to the Microsoft Visual Studio .NET toolbox, follow these steps:
 * 1) Start Microsoft Visual Studio .NET.
 * 2) On the View menu, click Toolbox.
 * 3) Right-click the Web Forms tab in the toolbox, and then click Customize Toolbox.

- or -

In Visual Studio 2003 .NET:

Right-click the Web Forms tab in the toolbox, and then click Add/Remove items.
 * 1) Click the .NET Framework Components tab. Notice that this tab lists at least two TreeView entries. One TreeView entry is for Windows applications such as .exe files. Another TreeView entry is the Internet Explorer WebBrowser TreeView control.
 * 2) View the Namespace column to identify the TreeView WebBrowser control with the Microsoft.Web.UI.WebControls namespace. Click to select the check box for this TreeView control, and then click OK.

back to the top

Create the Visual Basic .NET Project and Add the TreeView Control
 Start Visual Studio .NET. On the File menu, point to New, and then click Project. Click Visual Basic Projects under Project Types, click ASP.NET Web Application under Templates, and then click OK. On the Project menu, click Add Web Form. In the Name box, type a name for the Web Form, and then click Open. Follow these steps to add a TreeView control to the Web Form:  On the View menu, click Toolbox.</li> Click the Web Forms tab.</li> Locate the TreeView control. You may have to scroll through the list to locate the TreeView control.</li> Drag the TreeView control from the toolbox to your Web Form. By default, the control is named TreeView1.</li></ol> </li> Click 'TreeView1' on the Web Form, and then press F4 to see the Properties window.</li> Locate the AutoPostBack property in the Properties window. Set this property according to the following guidelines: <ul> Set AutoPostBack to True if you are not sending all of the data immediately (for example, if you are sending only the root nodes and particular child nodes). This option posts to the server every time the user changes the selection, expands a node, or collapses a node.</li> Set AutoPostBack to False if you are sending all of the data immediately (for example, for the sample in the Send All Data Immediately section). With this option, most activity remains on the client.</li></ul> </li></ol>

back to the top

Send All Data Immediately
<ol> Press F7 to open the code-behind window for the page.</li>  Add the following code to the top of the code-behind window, before the Public Class declaration: Imports System.Data Imports System.Data.SqlClient Imports Microsoft.Web.UI.WebControls </li>  Replace the code in the Page_Load event with the following code: Private Sub Page_Load(ByVal sender As System.Object, _           ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then ' You may have to change the ConnectionString to            ' access the Pubs database on your server. Dim strConnectionString As String = &quot;data source=(local);&quot; _ & &quot;initial catalog=pubs;integrated security=SSPI&quot; Dim strSQLAuthors As String = &quot;SELECT authors.au_id, &quot; _ & &quot;authors.au_lname, authors.au_fname FROM &quot; _ & &quot;authors ORDER BY authors.au_lname, &quot; _ & &quot;authors.au_fname, authors.au_id&quot; Dim strSQLTitles As String = &quot;SELECT titleauthor.au_id, &quot; _ & &quot;titles.title FROM titleauthor INNER JOIN titles &quot; _ & &quot;ON titleauthor.title_id = titles.title_id &quot; _ & &quot;ORDER BY titles.title&quot; Dim nodeAuthor As TreeNode Dim nodeTitle As TreeNode Dim cnn As SqlConnection Dim cmAuthors As SqlDataAdapter Dim cmTitles As SqlDataAdapter Dim ds As DataSet Dim rowAuthor As DataRow Dim rowTitle As DataRow

'Create the Connection and the DataSet object, 'and then open the connection. cnn = New SqlConnection(strConnectionString) ds = New DataSet cnn.Open

'Add an Authors table to the DataSet. cmAuthors = New SqlDataAdapter(strSQLAuthors, cnn) cmAuthors.Fill(ds, &quot;Authors&quot;)

'Add a Titles table to the DataSet. cmTitles = New SqlDataAdapter(strSQLTitles, cnn) cmTitles.Fill(ds, &quot;Titles&quot;)

'Create a relation between the Authors and the Titles tables. ds.Relations.Add(&quot;AuthorTitle&quot;, _                   ds.Tables(&quot;Authors&quot;).Columns(&quot;au_id&quot;), _                    ds.Tables(&quot;Titles&quot;).Columns(&quot;au_id&quot;))

'Populate the TreeView from the DataSet. For Each rowAuthor In ds.Tables(&quot;Authors&quot;).Rows nodeAuthor = New TreeNode nodeAuthor.Text = rowAuthor(&quot;au_lname&quot;) & &quot;, &quot; _ & rowAuthor(&quot;au_fname&quot;) TreeView1.Nodes.Add(nodeAuthor) For Each rowTitle In rowAuthor.GetChildRows(&quot;AuthorTitle&quot;) nodeTitle = New TreeNode nodeTitle.Text = rowTitle(&quot;Title&quot;) nodeAuthor.Nodes.Add(nodeTitle) Next Next

'Clean up. ds.Dispose cmAuthors.Dispose cmTitles.Dispose cnn.Close cnn.Dispose End If   End Sub </li></ol>

back to the top

Send Only Root Nodes and Requested Child Nodes
<ol> Press F7 to open the code-behind window for the page.</li>  Add the following code to the top of the code-behind window, before the Public Class declaration: Imports System.Data Imports System.Data.SqlClient Imports Microsoft.Web.UI.WebControls </li>  Replace the code in the Page_Load event with the following two procedures: Private Sub Page_Load(ByVal sender As System.Object, _           ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then 'You may have to change the ConnectionString to access the 'Pubs database on your server. Dim strConnectionString As String = &quot;data source=(local);&quot; _ & &quot;initial catalog=pubs;integrated security=SSPI&quot; Dim strSQLAuthors As String = &quot;SELECT authors.au_id, &quot; _ & &quot;authors.au_lname, authors.au_fname FROM &quot; _ & &quot;authors ORDER BY authors.au_lname, &quot; _ & &quot;authors.au_fname, authors.au_id&quot; Dim nodeAuthor As TreeNode Dim cnn As SqlConnection Dim cmAuthors As SqlDataAdapter Dim ds As DataSet Dim rowAuthor As DataRow

'Create the Connection object and the DataSet object, 'and then open the connection. cnn = New SqlConnection(strConnectionString) ds = New DataSet cnn.Open

'Add an Authors table to the DataSet. cmAuthors = New SqlDataAdapter(strSQLAuthors, cnn) cmAuthors.Fill(ds, &quot;Authors&quot;)

'Populate the TreeView from the DataSet. For Each rowAuthor In ds.Tables(&quot;Authors&quot;).Rows nodeAuthor = New TreeNode nodeAuthor.Text = rowAuthor(&quot;au_lname&quot;) & &quot;, &quot; _ & rowAuthor(&quot;au_fname&quot;) nodeAuthor.NodeData = rowAuthor(&quot;au_id&quot;) nodeAuthor.Expandable = ExpandableValue.CheckOnce TreeView1.Nodes.Add(nodeAuthor) Next

'Clean up. ds.Dispose cmAuthors.Dispose cnn.Close cnn.Dispose End If   End Sub

Private Sub TreeView1_Expand(ByVal sender As Object, ByVal e As _           Microsoft.Web.UI.WebControls.TreeViewClickEventArgs) _ Handles TreeView1.Expand Dim strConnectionString As String = &quot;data source=(local);&quot; _ & &quot;initial catalog=pubs;integrated security=SSPI&quot; Dim strSQLTitles As String Dim cnn As SqlConnection Dim cmTitles As SqlDataAdapter Dim ds As DataSet Dim rowTitle As DataRow Dim nodeTitle As TreeNode Dim nodeAuthor As TreeNode

nodeAuthor = sender.nodes(e.Node.ToString) If nodeAuthor.Nodes.Count = 0 Then

'Create the Connection object and the DataSet object, 'and then open the connection. cnn = New SqlConnection(strConnectionString) ds = New DataSet cnn.Open

'Add a Titles table to the DataSet. strSQLTitles = &quot;SELECT titles.title FROM titles &quot; _ & &quot;INNER JOIN titleauthor ON titles.title_id &quot; _ & &quot;= titleauthor.title_id where titleauthor.au_id &quot; _ & &quot;= '&quot; & nodeAuthor.NodeData.ToString _ & &quot;' ORDER BY titles.title&quot; cmTitles = New SqlDataAdapter(strSQLTitles, cnn) cmTitles.Fill(ds, &quot;Titles&quot;)

For Each rowTitle In ds.Tables(&quot;Titles&quot;).Rows nodeTitle = New TreeNode nodeTitle.Text = rowTitle(&quot;Title&quot;) nodeAuthor.Nodes.Add(nodeTitle) Next

'Clean up. ds.Dispose cmTitles.Dispose cnn.Close cnn.Dispose End If   End Sub </li></ol>

back to the top

Send Only Root Nodes and Minimum Child Nodes
<ol> Complete the steps in the Send Only Root Nodes and Requested Child Nodes section.</li>  Add one more procedure to the code: Private Sub TreeView1_Collapse(ByVal sender As Object, ByVal e As _           Microsoft.Web.UI.WebControls.TreeViewClickEventArgs) _ Handles TreeView1.Collapse Dim nodeAuthor As TreeNode nodeAuthor = sender.nodes(e.Node.ToString) If nodeAuthor.Nodes.Count > 0 Then nodeAuthor.Nodes.Clear nodeAuthor.Expandable = ExpandableValue.Always End If   End Sub </li></ol>

back to the top

<div class="references_section">