Click here to Skip to main content
Click here to Skip to main content

Dynamically populating a Microsoft Internet Explorer TreeView Web Control from a Database using SQL Server 2000 & XML Explicit

By , 20 May 2003
 

Introduction

Microsoft has made available the Internet Explorer Web controls which are not included in the framework by default. The assembly includes several controls one of which is the TreeView control which is discussed in this article. I will not be discussing how to use the control other than dynamically loading it with nodes using SQL Server's “Explicit mode”, and having each of the nodes' NavigateUrl attribute set to call a JavaScript function. I shall also not be going into any depth of discussion on SQLXML, as it is way beyond the scope of the article. I do however provide links to some further information on the topic. I shall however discuss SQLXML in a bit more depth in a future article “DataBinding with SQLXML”. See the Microsoft documentation for further programming of the TreeView control.

Where to find the IE Web controls download

Whilst it can be very handy to bind an XML document to the TreeView control, I often face the requirement to do this on the fly, to accommodate dynamic changes to the TreeView's underlying data structure as required by the most current business model. One way to achieve this dynamic output is to use SQL Server's built in XML support in combination with the TreeNodeSrc attribute of the TreeView Control. For example, recently I needed to provide a known and commonly used hierarchical diagnosis path for help desk operators to select whilst servicing a customer. The output of this looked something like this:

TreeView

Writing the query to return XML

SQL Server 2000 provides excellent XML support, providing XML output from SQL Queries using ‘Modes’ such as explicit, Auto and Raw. Our example requires the use of the explicit mode to produce a format which MS calls the universal table, which will give us the output we require. For more on returning XML from TSQL queries see: SQL Server Books on Line index, keyword XML, EXPLICIT or:

I chose to represent the data recursively in the database using a single reflexive table to store the hierarchies. Each row fldDiagnosisParentId references another row fldDiagnosisId except for those rows where fldTierNumber is equal to a value of 1. Run the attached download SQLXML file to produce this table and fill it with data.

Sample screenshot

Run the attached download SQLXML file to produce this table and fill it with data.

Sample screenshot

Next we need the query to produce the output desired.

create procedure spReflexiveNodesText
                as
select
                1 as Tag,
                0 as Parent,
                null as [TREENODES!1!text],
                null as [treenode!2!text],
                null as [treenode!2!NavigateUrl],
                null as [treenode!3!text],
                null as [treenode!3!NavigateUrl],
                null as [treenode!4!text],
                null as [treenode!4!NavigateUrl]
union 
                all
select
                2 as Tag,
                1 as Parent,
                null,
                fldDiagnosisLabel as [treenode!2!text],
                'Javascript:FillBox("' + 
                     cast(fldDiagnosisId as varchar(10)) + '")'
                     as [treenode!2!NavigateUrl],
                null as [treenode!3!text],
                null as [treenode!3!NavigateUrl],
                null as [treenode!4!text],
                null as [treenode!4!NavigateUrl]
from
                tblDiagnosis
where
                fldTierNumber = 1
union
                all
select
                3 as Tag,
                2 as Parent,
                null,
                t1.fldDiagnosisLabel,
'Javascript:FillBox("' + cast(t1.fldDiagnosisId as varchar(10)) + '")',
                t2.fldDiagnosisLabel as [treenode!3!text],
                'Javascript:FillBox("' + 
                cast(t2.fldDiagnosisId as varchar(10)) + '")'
                as [treenode!3!NavigateUrl],
                null,
                null
from
                tblDiagnosis as t1
inner join
                tblDiagnosis as t2
on
                t1.fldDiagnosisId = t2.fldDiagnosisParentId
where
                t1.fldTierNumber = 1
and
                t2.fldTierNumber = 2
union
                all
select
                4 as Tag,
                3 as Parent,
                null,
                t1.fldDiagnosisLabel,
'Javascript:FillBox("' + cast(t1.fldDiagnosisId as varchar(10)) + '")',
                t2.fldDiagnosisLabel,
'Javascript:FillBox("' + cast(t2.fldDiagnosisId as varchar(10)) + '")',
                t3.fldDiagnosisLabel as [treenode!4!text],
                'Javascript:FillBox("' + 
                         cast(t3.fldDiagnosisId as varchar(10)) + '")'
                         as [treenode!4!NavigateUrl]
from
                tblDiagnosis as t1
inner join
                tblDiagnosis as t2
on
                t1.fldDiagnosisId = t2.fldDiagnosisParentId
inner join
                tblDiagnosis as t3
on
                t2.fldDiagnosisId = t3.fldDiagnosisParentId
where
                t1.fldTierNumber = 1
and
                t2.fldTierNumber = 2
and
                t3.fldTierNumber = 3
order by
                [TREENODES!1!text],
                [treenode!2!text],
                [treenode!3!text],
                [treenode!4!text]
for 
                xml explicit

If you run this query in query analyzer and remove the for xml explicit directive you will get what MS calls a universal table output (which can be handy for debugging). If you run the query with the for xml explicit directive, the output will be a long XML string like that listed below. The output listed highlights the fact that our query in this instance is producing the required XML structure required by the TreeView control and has taken advantage of the treenode elements NavigateUrl attribute which will call a JavaScript function in the page containing the TreeView.

<TREENODES>
<treenode text="Modem" NavigateUrl="Javascript:FillBox("3")">
<treenode text="Cable" NavigateUrl="Javascript:FillBox("7")">
<treenode text="Broken" NavigateUrl="Javascript:FillBox("11")"/>
</treenode>
</treenode>
<treenode text="Monitor" NavigateUrl="Javascript:FillBox("4")">
<treenode text="Fuzzy" NavigateUrl="Javascript:FillBox("8")">
<treenode text="Driver" NavigateUrl="Javascript:FillBox("12")"/>
</treenode>
</treenode>
<treenode text="PC" NavigateUrl="Javascript:FillBox("1")">
<treenode text="Ram" NavigateUrl="Javascript:FillBox("5")">
<treenode text="Faulty" NavigateUrl="Javascript:FillBox("9")"/>
</treenode>
</treenode>
<treenode text="Printer" NavigateUrl="Javascript:FillBox("2")">
<treenode text="Cartridges" NavigateUrl="Javascript:FillBox("6")">
<treenode text="Empty" NavigateUrl="Javascript:FillBox("10")"/>
</treenode></treenode>
</TREENODES>

Note: There are as many self joins required as there are tier levels, which is not exactly dynamic, meaning that if you add a tier to the data in the table, then you need to change the query. This can be overcome by first discovering the max() tier value found in the table and building a SQL statement up in a string and then executing the statement contained therein using the sp_executesql system stored procedure. The first select statement in the Union query is required to give us the root node “TREENODES” which is required by the TreeView Control. The text attributes of each treenode element is what provides each of the TreeView node's individual display labels and the NaviateUrl is commonly used to navigate to another HTTP location or in this instance passing control to a JavaScript function to produce some further processing.

For more info on XML modes available in SQL Server 2000 check out this link. This link provides a sample chapter of the book “Programming Microsoft® SQL Server™ 2000 with XML” by Graeme Malcolm and published by the MS press.

Setting up SQL Server for access through a URL

The next step in making the query work for us is setting up a SQL Server 2000 virtual directory that will allow us to call the stored procedure using a URL string. When SQL Server is setup, it should configure a menu item in its start menu folder labeled “Configure SQL XML support in IIS”.

IIS Virtual Directory

Once this is complete you will be able to access the stored procedure from Internet Explorer using the URL: http://localhost/TreeSqlXml?sql=execute+spReflexiveNodesText.

For more information on how to set up the virtual directory see: SQL Server Books or try MSDN website.

For more on how to set up SQL Server for querying a database with a URL string see: Books on Line and use index keyword ‘XML, executing stored procedures at URL’ or try MSDN website.

The ASPX page's HTML code.

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ReflexiveTreeNodes</title>
<metaname="GENERATOR"Content="Microsoft Visual Studio 7.0">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript" content="JavaScript">
<metaname="vs_targetSchema" 
    content="http://schemas.microsoft.com/intellisense/ie5">
<scriptlanguage="javascript">
<!--
/* The Function specified in the output
   of the stored procedure 
   SEE the NavigateUrl attribute
   Of each treenode element  */
function FillBox(val)
       {
              alert(val);
       }
//-->
</script>
</HEAD>
<bodyMS_POSITIONING="GridLayout">
       <formid="ReflexiveTreeNodes" method="post" runat="server">
       <!--Note the URL string used as the value of the TreeNodeSrc -->
       <iewc:TreeViewid="TreeView1"
       style="Z-INDEX: 101; LEFT: 6px; POSITION: absolute; TOP: 8px"
runat="server" 
TreeNodeSrc="http://localhost/TreeSqlXml?sql=execute+spReflexiveNodesText">
</iewc:TreeView>
       </form>
</body>
</HTML>

NOTE: Remember to reference the Microsoft.Web.UI.WebControls.dll in your project and include the URL to the stored procedure as the value for your TreeView tag's TreeNodeSrc attribute.

Caching

Choose the most appropriate caching policy for your page based on the expected frequency of change to underlying database table and whether or not any parameters are passed from the application to the stored procedure. Bare in mind that the stored procedure will fetch data from the database or cache, dependant on whether the request for data is already cached.

Finally

It is also worth mentioning that the TreeView control can also be populated by passing the fully qualified path of an XML file to the TreeNodeSrc attribute.

TreeNodeSrc="myTreeViewFile.xml"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Simon Segal
Chief Technology Officer
Australia Australia
Member
Simon Segal resides in Melbourne Australia, is a certified MCAD, MCSD, MCDBA, MCSE, MCST BizTalk Specialist and has been working in the Software Development industry for some 10 years now. His key area of interest are distributed systems / SOA built with Microsoft technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCopying a tree view into gmail compose boxmemberrockyurock27 Feb '10 - 7:20 
Hello guys,
 
can anybody suggest me a way how can i copy a tree view (or else can create a tree view) to my gmail compose box.
i have created a simple .dot file (document template) and wants to use in gmail compose box.
to date i did not get any solution for this.
 
hope t hear from you...
 

/rocky
GeneralFind me here....memberSimon Segal20 Jun '08 - 21:40 
Find me here
 
Simon Segal

GeneralOpensource PHP DBTreeViewmemberRodolphe Cardon20 Jul '07 - 10:08 
The opensource AJAX tree view with database link.
 
http://rcardon.free.fr/dbtreeview/
 
PHP DBTreeView is a useful web control to display information in a tree like windows explorer. This component is designed to extract dynamic content from database, so that the tree content is updated step by step.
 
This project uses the latest web technologies like Ajax (Javascript, DHTML, XMLHttpRequest) and PHP 5.
 
Rodolphe
QuestionWhat about n-tier relationshipmemberHirenjPatel12 Oct '06 - 0:05 
What can i do to upgrade this stored procedure to get n-tier relationship from database
 
I used this one got only three stage depth but if i want depth of n level than what can i do
 
please help me
 

 
Hiren Patel
GeneralGot error message !memberKhinLLK15 Aug '06 - 21:49 
Hi ,
 
I got the following error messge when I try out the sample that is shown.
"The data at the root level is invalid. Line 1, position 1."
Any idea for this?

GeneralMicrosoft.Web.UI.WebControls.dllmemberAmir K15 May '06 - 12:27 
After setting up IE Web Controls, Microsoft.Web.UI.WebControls.dll file is missing ! anybody have a clue why ? Or is there any altarnative way in order to replace the file ?
Thank you.
GeneralRe: Microsoft.Web.UI.WebControls.dllmemberSimon Segal5 May '06 - 12:52 
You need to run the Build.bat in the location you installed the IE Web Controls. This will build the dll for you.
 
Simon Segal
GeneralRe: Microsoft.Web.UI.WebControls.dllmemberAmir K15 May '06 - 20:18 
As I explained in the previous posting, tried to do that yet the file is not created,wondering why ?
GeneralRe: Microsoft.Web.UI.WebControls.dllmemberSimon Segal6 May '06 - 1:47 
CSC.exe is the csharp compiler. Run the Build.bat in the context of either the SDK command prompt in your start menu or use cmd.exe and navigate to the directory where the bat file is located and run it. CSC.exe is not recognized because its path is not part of the environment PATH variable in Windows.
 
Simon Segal
Questionhow to find offsetX and offsetX of treenodesussAnonymous12 Oct '05 - 6:26 
Hello,
 
I need to edit text in tree node like editlabel in window form. I try to use java script to show input text at the position of treenode. and when user input data, I will set the input string to treenode.text but, I can't find the X and Y position.
Can you help me.
 
Suthep.
 

QuestionUpdating the tree back into databasememberManjesh123422 Sep '05 - 18:20 
how can we update the tree back into database,
for example if we have
 

 
we can display some information via checkbox ie either it is checked or unchecked, if we uncheck the box how can we update the info in database.
 

AnswerRe: Updating the tree back into databasesussAnonymous23 Sep '05 - 1:32 
Postback
GeneralRe: Updating the tree back into databasemembermanjesh12323 Sep '05 - 14:09 
by postback?, but what are the properties or functions in the treeview which can store the information from the nodes back to the database if any changes are made, is this available in microsoft treeview.
 
I am able to display all the information, but when I postback it will retain its display but it will not update it on database! I want the changes made to be updated in the database.

GeneralNew Download Location for Web Control DownloadmemberSimon Segal8 Jun '04 - 21:51 
MS keep moving it around.....
 
IE Web Controls are here
 
Simon Segal
GeneralIE TreeView Controlmemberchintan19791 Jun '04 - 18:00 
Hi,
 
I want to use IE Treeview control in my ASP.NET application. Can any one guide me how can I get this control and how can I use it? I want to populate my treeview nodes based on a sql database table.
 
Please some on guide me its urgent.
 
Thanks.
 
Chintan
GeneralRe: IE TreeView ControlmemberSimon Segal8 Jun '04 - 21:49 
IE Web Controls
 
Simon Segal
QuestionHow can I get rid of the backgroud colour of the first node?memberpllms13 May '04 - 15:48 
Hi,
 
I realised that the first node of the Tree always has a background colour. How can I get rid of the backgroud colour of the first node?
 

Thanks.
pllms Blush | :O
 


QuestionCan I use TreeView Web Control in Dreamweaver?memberpllms12 May '04 - 23:09 
Hi,
 
I am just curious if I can code my TreeView WebControl using Dreamweaver instead of VS.NET?
 

Thanks & Regards,
pllms
AnswerRe: Can I use TreeView Web Control in Dreamweaver?memberSimon Segal12 May '04 - 23:17 
I havent tried, but I cant see why not. I dont know if you will get the
drag and drop support for it that VS.Net will give however. The proof will
be in the trying. Sorry I couldnt tell you more as I am not a Dreamweaver user
at all.

 
Simon Segal
GeneralRe: Can I use TreeView Web Control in Dreamweaver?memberravijikijai1 Feb '06 - 3:16 
You can use drag and drop functionalty with Tree view control using java script.Smile | :)
GeneralAdding data to treeview in Windows Formsmembersasijrao14 Apr '04 - 4:12 
Hi all,
 
I have a table with four columns
 
Ex:
 
JObid - Autofield
PArentJOBid --- It contains values in the field Folderid
Jobname
JobType
 
My problem is that
when I get data from DB i shoul load it in treeview
 
Ex data
 
JObid   PArentJOBid   Jobname             JobType
1     null     Root          Root
2     1     job2          job
3     1     job3          job
4     2     job2-part1          part
5     2     job2-part3      PArt
6     3     job3-part1          PArt
7     3     job3-part2          part
8     4     job2-part1-sub1     Subpart
9     4     job2-part3-sub1     Subpart
10     9     job2-part3-sub1-1     finsubpart
11     9     job2-part3-sub1-2     finsubpart
 
the tree should look like this..
 
Root
   |- JOb2
         |- job2-part1    
                  |- job2-part1-sub1
                  |- job2-part3-sub1
                           |- job2-part3-sub1-1
                           |- job2-part3-sub1-2
         |- job2-part2
   |- JOb3
         |- job3-part1    
         |- job3-part2
 
Any thoughts Guidance or examples you have?
 


GeneralSetting a node's tag propertymemberMSDevMan25 Mar '04 - 7:30 
Hello, I liked your article! It was exactly what I needed but I want to set the Node.tag property with another HTML string that dynamically builds my Web Server controls when a node is selected. Can I build the appropriate string and assign it in this stored procedure like you did the NAVIGATEURL? Please help me...
 
Here is the test select & the SQL Error I get:
select 1 as Tag, 0 as Parent,
null as [TREENODES!1!text],
null as [treenode!2!text], null as [treenode!2!NavigateUrl],
null as [treenode!2!Attributename!tag],
null as [treenode!3!text], null as [treenode!3!NavigateUrl]
,null as [treenode!3!Attributename!tag]
--First select all the Report Groups
union all
select 2 as Tag, 1 as Parent, null,
GrpName as [treenode!2!text],
null as [treenode!2!NavigateUrl],
null as [treenode!2!Attributename!tag],
null as [treenode!3!text], null as [treenode!3!NavigateUrl]
,null as [treenode!3!Attributename!tag]
from Lynks_RptGroups
--Second select all the Reports for each of the Groups
union all
select 3 as Tag, 2 as Parent,
null,
g.GrpName as [treenode!1!text],
null as [treenode!2!NavigateUrl],
null as [treenode!2!Attributename!tag],
x.NodeName as [treenode!3!text],
null as [treenode!3!NavigateUrl]
,'TEST TAG' as [treenode!3!Attributename!tag]
from Lynks_RptXref x
inner join Lynks_RptGroups g on x.GrpID = g.GrpID
order by [TREENODES!1!text], [treenode!2!text], [treenode!3!text]
for XML EXPLICIT

GENERATES FOLLOWING ERROR:
Server: Msg 6824, Level 16, State 1, Line 1
In the FOR XML EXPLICIT clause, mode 'tag' in a column name is invalid.
 

Timothy Shaffer
Senior Systems Developer
Lynks Technology & Mgt Services, Inc.
330/342-7807
GeneralRe: Setting a node's tag propertymemberSimon Segal25 Mar '04 - 12:43 
Looking at it briefly I would say that
the keyword Tag is your problem. Try not
using it like this
null as [treenode!3!Attributename!tag]
and maybe try something like
null as [treenode!3!Attributename!tagProperty]
 


 
Simon Segal
GeneralRe: Setting a node's tag propertymemberMSDevMan26 Mar '04 - 9:51 
Sorry, I still get the SQL error. You can test it in Query Analzer if you simple replace the table refs with constants. Where did you find the syntax for binding the treeview to an XML stream?

 
Timothy Shaffer
Senior Systems Developer
Lynks Technology & Mgt Services, Inc.
330/342-7807
GeneralRe: Setting a node's tag propertymemberSimon Segal30 Mar '04 - 23:43 
Sorry but I looked at this a bit too quickly last time. I think your use of AttributeName argument is
causing your problem. You actually want an attribute like NavigateUrl for Tag. The syntax for xml stream came somewhere from the MS Site but I cant remember exactly where.
 
I think this is what your after (except for the missing table refs)
 
select 1 as Tag, 0 as Parent,
null as [TREENODES!1!text],
null as [treenode!2!text], null as [treenode!2!NavigateUrl],
null as [treenode!2!tag],
null as [treenode!3!text], null as [treenode!3!NavigateUrl]
,null as [treenode!3!tag]
--First select all the Report Groups
union all
select 2 as Tag, 1 as Parent, null,
null as [treenode!2!text],
null as [treenode!2!NavigateUrl],
null as [treenode!2!tag],
null as [treenode!3!text], null as [treenode!3!NavigateUrl]
,null as [treenode!3!tag]
--from Lynks_RptGroups
--Second select all the Reports for each of the Groups
union all
select 3 as Tag, 2 as Parent,
null,
null as [treenode!1!text],
null as [treenode!2!NavigateUrl],
null as [treenode!2!tag],
null as [treenode!3!text],
'some url' as [treenode!3!NavigateUrl]
,'test tag' as [treenode!3!tag]
 
order by [TREENODES!1!text], [treenode!2!text], [treenode!3!text]
for XML explicit
 
Simon Segal

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 21 May 2003
Article Copyright 2003 by Simon Segal
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid