Microsoft KB Archive/320909

= How to dynamically add nodes to a TreeView WebBrowser control by using Visual C# .NET =

Article ID: 320909

Article Last Modified on 4/21/2006

-

APPLIES TO


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

-



This article was previously published under Q320909





For a Microsoft Visual Basic .NET version of this article, see 319441.



SUMMARY
This step-by-step article demonstrates how to add multiple levels of nodes to a Microsoft Internet Explorer TreeView WebControl 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.

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

http://msdn.microsoft.com/downloads/samples/internet/ASP_DOT_NET_ServerControls/WebControls/default.asp

Add the TreeView WebControl to the Visual Studio .NET toolbox
To add the TreeView WebControl 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.

In Visual Studio .NET 2003:

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 TreeView WebControl.
 * 2) View the Namespace column to identify the TreeView WebControl with the Microsoft.Web.UI.WebControls namespace. Click to select the check box for this TreeView WebControl, and then click OK.

Create the Visual C# .NET project and add the TreeView WebControl
 Start Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual C# 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 WebControl to the Web Form: <ol style="list-style-type: lower-alpha;"> On the View menu, click Toolbox.</li> Click the Web Forms tab.</li> Locate the TreeView WebControl. You may have to scroll through the list to locate the TreeView control.</li> Drag the TreeView WebControl 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>

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 namespace: using System.Data.SqlClient; using Microsoft.Web.UI.WebControls; </li>  Replace the code in the Page_Load event with the following code: private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) {

// You may have to change the ConnectionString to    // access the Pubs database on your server. string strConnectionString  = &quot;data source=(local);&quot; + &quot;initial catalog=pubs;integrated security=SSPI&quot;; string strSQLAuthors = &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;; string strSQLTitles = &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;; TreeNode nodeAuthor; TreeNode nodeTitle; SqlConnection cnn; SqlDataAdapter cmAuthors; SqlDataAdapter cmTitles; DataSet ds; //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. foreach (DataRow 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); foreach (DataRow rowTitle in                                rowAuthor.GetChildRows(&quot;AuthorTitle&quot;)) {           nodeTitle = new TreeNode; nodeTitle.Text = rowTitle[&quot;Title&quot;].ToString; nodeAuthor.Nodes.Add(nodeTitle); }   }

//Clean up. ds.Dispose; cmAuthors.Dispose; cmTitles.Dispose; cnn.Close; cnn.Dispose; } } </li></ol>

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 namespace: using System.Data.SqlClient; using Microsoft.Web.UI.WebControls; </li>  Replace the code in the Page_Load event with the following two procedures: private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) {           //You may have to change the ConnectionString to access the //Pubs database on your server. string strConnectionString  = &quot;data source=(local);&quot; + &quot;initial catalog=pubs;integrated security=SSPI&quot;; string strSQLAuthors = &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;; TreeNode nodeAuthor; SqlConnection cnn; SqlDataAdapter cmAuthors; DataSet ds; //Create the Connection and the DataSet, 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. foreach (DataRow 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;].ToString; nodeAuthor.Expandable = ExpandableValue.CheckOnce; TreeView1.Nodes.Add(nodeAuthor); }

//Clean up. ds.Dispose; cmAuthors.Dispose; cnn.Close; cnn.Dispose; } } private void TreeView1_Expand( object sender, Microsoft.Web.UI.WebControls.TreeViewClickEventArgs e) { string strConnectionString  = &quot;data source=(local);&quot; + &quot;initial catalog=pubs;integrated security=SSPI&quot;; string strSQLTitles; SqlConnection cnn; SqlDataAdapter cmTitles; DataSet ds; TreeNode nodeTitle; TreeNode nodeAuthor; nodeAuthor = ((Microsoft.Web.UI.WebControls.TreeView)sender).Nodes[(int.Parse(e.Node))]; if (nodeAuthor.Nodes.Count == 0) {       //Create the Connection and the DataSet, 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;);

foreach (DataRow rowTitle in ds.Tables[&quot;Titles&quot;].Rows) {           nodeTitle = new TreeNode; nodeTitle.Text = rowTitle[&quot;Title&quot;].ToString; nodeAuthor.Nodes.Add(nodeTitle); }

//Clean up. ds.Dispose; cmTitles.Dispose; cnn.Close; cnn.Dispose; } } </li>  Add the following code to the OnInit procedure: TreeView1.Expand += new ClickEventHandler(this.TreeView1_Expand); </li></ol>

Send only root nodes and minimum child nodes
<ol> Complete the steps in the Send only root nodes and requested child nodes section.</li> <li> Add one more procedure to the code: private void TreeView1_Collapse(object sender, Microsoft.Web.UI.WebControls.TreeViewClickEventArgs e ) {   TreeNode nodeAuthor; nodeAuthor = ((Microsoft.Web.UI.WebControls.TreeView)sender).Nodes[(int.Parse(e.Node))]; if (nodeAuthor.Nodes.Count > 0) {       nodeAuthor.Nodes.Clear; nodeAuthor.Expandable = ExpandableValue.Always; } } </li> <li> Add the following code to the OnInit procedure: TreeView1.Collapse +=new ClickEventHandler(this.TreeView1_Collapse); </li></ol>

<div class="references_section">