ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
Sams Teach Yourself C# Web Programming in 21 Days
Sams Teach Yourself C# Web Programming in 21 Days

Find Prices
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software
Developing with the Treeview Web Control Part1 : The Basics

Introduction

Have you ever wanted to create an expandable tree much like the one found at Microsoft's MSDN site, or the one found in windows explorer? If so then this article should interest you. Creating an expandable tree has been made much easier with the IE web controls for asp.net. These controls are a free download from Microsoft and provide us with a quick and easy way to present hierarchical data in an expandable tree.

One of the best things about the control treeview is its ability to serve browser specific html. If the browser is a pre IE 5.5 browser then the treeview generates html 3.2 code whereas if it is an upper-level browser, Internet Explorer 5.5 and above, then it creates rich dhtml behaviors to enhance both the esthetics and usability of the controls. This means that with older browsers, all the work will be done server-side and a post back to the server will be required when they expand or collapse a node. With upper level browsers a lot more of the work is done on the client side with dhtml, saving the user another trip back to the server. This makes the controls 100% browser compatible, but the best user experience will be had using IE 5.5 or above.

In this article we are going to cover the basics of creating a simple treeview and customizing its appearance and behavior. Our goal is to create a tree that will link to sql server and asp.net related sites.

Pre-Requisites

You should have at least a basic understanding of asp.net and web forms. You will also need a system with the .net framework and the IE web controls installed. To install the IE Web controls click here.

Creating a simple treeview

We are going to create a simple .aspx file called treeview.aspx. The first thing we need to do is add import and register directives to the top of our page.

<%@ Register TagPrefix="mytree" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.116, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ import namespace="Microsoft.Web.UI.WebControls" %>

The import directive tells the system that we are going to be using the webcontrols namespace on our page. The register directive creates the tag mytree for us to use when we create out tree. These two directives will need to be included on any pages where you are going to be using the treeview. Now we are going to add the basic html requirements of the page and a server-side form that will hold our treeview.

<html>
   <head>
   </head>
      <body>
         <form runat="server">
         </form>
      </body>
</html>

Inside our server side form we are going to add our treeview using the mytree tag that we registered at the top of our page.

<form runat="server">
   <mytree:treeview runat="server" id="oTree">
      <mytree:treenode text="ASP.Net Sites">
         <mytree:treenode text="AspAlliance" navigateurl="http://www.aspalliance.com" />
      </mytree:treenode>
   </mytree:treeview>
</form>

The two elements that we are using in the above example are treeview and treenode. The treeview element is used to start and end the complete tree structure. This is also where we are going to set a lot of our properties. The treenode element represents a node on our tree. You will notice that the first treenode contains the second treenode, this causes the second tree node to be a child node to the first node. Also notice we are using the text property of the treenode element to specify the text associated with the node. The text property can contain html and is actually the only place you can use html code inside the treeview tags. Also notice in the code above that we are using the navigateurl property to define what to do when this node is selected. When the nodes are selected the current page will change to the url that we defined.

But as you can see this treeview is not very pretty at all, so now we are going to explore some of the different stylistic properties that we can set. We are also going to add some more nodes to better represent our final tree. In this next treeview we are going to use the treenodetype element to define how our treenodes will appear. The TreeNodeType is a way of defining a type or class that nodes can inherit from.

<form runat="server">
<mytree:treeview runat="server" id="oTree" ChildType="Folder">
   <mytree:treenodetype Type="Folder" ExpandedImageUrl="images/openfolder.gif" ImageUrl="images/closedfolder.gif" />
   <mytree:treenodetype Type="Link" ImageUrl="images/link.gif" />
   <mytree:treenode text="ASP.Net Sites" ChildType="Link">
      <mytree:treenode text="ASP Alliance" navigateurl="http://www.aspalliance.com" />
      <mytree:treenode text="4GuysFromRolla" navigateurl="http://www.4guysfromrolla.com" />
      <mytree:treenode text="DotNetJunkies" navigateurl="http://www.navigateurl.com" />
   </mytree:treenode>
   <mytree:treenode text="Sql Server Sites" ChildType="Link">
      <mytree:treenode text="Swynk" navigateurl="http://www.swynk.com" />
      <mytree:treenode text="Sql-Server-Performance.com" navigateurl="http://www.sql-server-performance.com" />
   </mytree:treenode>
</mytree:treeview>
</form>

We create two different treenodetype elements in this example; one called folder and the other called link. You can name these anything you like, the name is just used to reference them in the rest of the tree. The two properties we use are ExpandedImageUrl and ImageUrl, these simply represent the images we want displayed when the node is expanded or collapsed. We then reference our treenodetype using the ChildType property and set which treenodetype we want the children on this node or tree to use. When using multiple treenodetypes you have to think of them like cascading style sheets, where the latest setting takes precedence. To better show this we are going to use the ImageUrl property of the 4guysfromrolla node to set it to a different image than rest of the links. This property setting will take precedence over the treenodetype setting.

<mytree:treenode text="ASP.Net Sites" ChildType="Link">
   <mytree:treenode text="ASP Alliance" navigateurl="http://www.aspalliance.com" />
   <mytree:treenode text="4GuysFromRolla" navigateurl="http://www.4guysfromrolla.com" ImageUrl="images/4guys.gif" />
   <mytree:treenode text="DotNetJunkies" navigateurl="http://www.navigateurl.com" />
</mytree:treenode>

I still think our tree needs a little bit more style. So we are going to use three other properties that are available to use. Default Style -Sets the default style of the node when it is not selected.
HoverStyle -Sets the style of the node when the mouse is over it.
SelectedStyle - Sets the style when the node has been selected.
These properties can be set on the tree , the node , or the treenodetype. In the next example we are going to show some of each.

<form runat="server">
<mytree:treeview runat="server" id="oTree" ChildType="Folder" SelectedStyle="background:ffffcf;color:black;text-decoration:underline" DefaultStyle="font-family:Verdana" HoverStyle="background:ffffff;color:#000080;text-decoration:underline" >
   <mytree:treenodetype Type="Folder" ExpandedImageUrl="images/openfolder.gif" ImageUrl="images/closedfolder.gif" />
   <mytree:treenodetype Type="Link" ImageUrl="images/ie.gif" HoverStyle="font-size:14" />
   <mytree:treenode text="ASP.Net Sites" ChildType="Link">
      <mytree:treenode text="ASP Alliance" navigateurl="http://www.aspalliance.com" />
      <mytree:treenode text="4GuysFromRolla" navigateurl="http://www.4guysfromrolla.com" ImageUrl="images/4guys.gif" />
      <mytree:treenode text="DotNetJunkies" navigateurl="http://www.navigateurl.com" />
   </mytree:treenode>
   <mytree:treenode text="Sql Server Sites" ChildType="Link" >
      <mytree:treenode text="Swynk" navigateurl="http://www.swynk.com" HoverStyle="Color:Red" />>
      <mytree:treenode text="Sql-Server-Performance.com" navigateurl="http://www.sql-server-performance.com" />
   </mytree:treenode>
</mytree:treeview>
</form>

In the above example we are using css attributes to change the stylistic properties of our tree. We set properties on the treeview, the treenodetype, and an individual treenode and you can see the effects of each in the example. This shows off the versatility and customization available with this control.

This is the conclusion of Part 1 of developing with the treeview web control.
Please check out Part 2 for information on dynamically creating treeviews.

 
 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/21/2009 5:51:34 PM