Click here to Skip to main content
15,881,812 members
Articles / Web Development / ASP.NET
Article

TreeWeb - custom ASP.NET control

Rate me:
Please Sign up or sign in to vote.
4.83/5 (67 votes)
2 Mar 20035 min read 364.2K   9.3K   188   113
A tree control that can be used in every website based on ASP.NET.

Sample Image - TreeWeb.gif

Introduction

There are not so many to say about this article. It simply offers an implementation for an ASP.NET custom tree control.

Almost three years ago I was trying to implement a tree control in raw asp (http://www.codeproject.com/asp/treecontrol.asp). At that time I realized how many things are missing the web developers that decided to implement their applications using Microsoft Technologies comparing with their colleagues that are using Java Technologies.

No longer after that I heard for the first time about the new technology (.NET) that Microsoft wants to have. From then I was trying in my free available time (which is not so much) to read about .NET and to keep track with the news about it. When I gathered enough information about .NET and the support it offers (not only) for the web applications developers on Microsoft platforms, I understood that finally they could compete with the Java web applications developers. With ASP.NET Microsoft jumped "tree big steps" ahead in supporting the web applications on Windows platforms.

UML Diagram

The diagram describes the basic architecture of the control. 

UML Diagram

iiuga.Web.UI.TreeWeb

TreeWeb class.

Properties

Image 3ElementsGets the elements collection of the tree.
Image 4CollapsedElementImageGets or sets the URL to the image that is showen for a collapsed element.
Image 5ExpandedElementImageGets or sets the URL to the image that is showen for an expanded element.
Image 6CheckBoxesGets or sets whether check boxes are displayed next to the tree elements in the treeweb control.
Image 7TargetGets or sets the default target for all tree elements action.
Image 8ImageListGets the collection of images urls for the TreeWeb control.
Image 9DefaultElementCssClassGets or sets the default CssClass for the tree elements.
Image 10DefaultElementCssClassOverGets or sets the default CssClass for the tree elements when OnMouseOver client event occure. [Not Implemented]

Methods
Image 11#ctorTreeWeb constructor
Image 12RaisePostBackEvent(IPostBackEventHandler.RaisePostBackEvent) Raise control specific events on post back.
Image 13LoadXMLLoads the tree structure from a XML file. [Not Implemented]
Image 14SaveXMLSaves the structure of the tree in a XML structure. [Not Implemented]
Image 15RenderRender the TreeWeb control.
Image 16SaveViewState(IStateManager.SaveViewState) Saves the changes of TreeWeb's view state to an Object.
Image 17LoadViewState(IStateManager.LoadViewState) Loads the TreeWeb's previously saved view state.
Image 18TrackViewState(IStateManager.TrackViewState) Instructs the TreeWeb to track changes to its view state.

Events
Image 19ExpandOccurs when an tree element is expanded.
Image 20CollapseOccurs when an tree element is collapsed.

iiuga.Web.UI.TreeElement

TreeElement class.

Properties

Image 21IDGets the ID of the TreeElement.
Image 22TextGets or sets the text to be displayed for the TreeElement.
Image 23ElementsGets the ElementsCollection for the TreeElement.
Image 24ParentGets the TreeElement parent object for the current element.
Image 25TreeWebGets the TreeWeb object.
Image 26IsExpandedGets whether the element is expanded or not.
Image 27HasElementsGets whether the element has or not children elements.
Image 28LevelGets the level where the element is in the tree.
Image 29IsCheckedGets whether the element is checked or not. [Not Implemented]
Image 30KeyGets or sets specific data for the tree element.
Image 31NavigateUrlGets or sets the action for the tree element.
Image 32TargetGets or sets the target of the tree element action.
Image 33CheckBoxGets or sets whether a check box is displayed next to the current element.
Image 34ToolTipGets or sets the ToolTip display text for the current element.
Image 35EnabledGets or sets whether the current element is enabled or not.
Image 36CssClassGets or sets the default CssClass for an element.
Image 37CssClassOverGets or sets the default CssClass for the element when OnMouseOver client event occure. [Not Implemented]
Image 38ImageIndexGets or sets the image index to be showen before the element in the tree.
Image 39IsTrackingViewState

Methods
Image 40#ctorTreeElement constructor.
Image 41#ctorTreeElement constructor.
Image 42#ctorTreeElement constructor.
Image 43ExpandExpand current element, it has effect only if it has elements.
Image 44CollapseCollapse current element, it has effect only if it has elements.
Image 45FindElementFinds a specified child element of current tree element.
Image 46SaveViewState(IStateManager.SaveViewState) Saves the changes of TreeElement's view state to an Object.
Image 47LoadViewState(IStateManager.LoadViewState) Loads the TreeElement's previously saved view state.
Image 48TrackViewState(IStateManager.TrackViewState) Instructs the TreeElement to track changes to its view state.

Using the code

... as any other web server control.

ASP.NET
//
// build the tree structure inside the web form
//

<%@ Register TagPrefix="iiuga" Namespace="iiuga.Web.UI" 
             Assembly="TreeWebControl" %>

...
    
<iiuga:treeweb id="_treeWeb" runat="server" CollapsedElementImage="plus.jpg" 
        ExpandedElementImage="minus.jpg">
<ImageList> 
    <iiuga:ElementImage ImageUrl="icon1.gif"></iiuga:ElementImage>
</ImageList>
<Elements>
    <iiuga:treeelement text="Your Online Radio Stations" 
           CssClass="Sample3_Header">
</iiuga:treeelement>
    <iiuga:treeelement text="Classic Rock & Oldie" 
           ToolTip="Classic Rock & Oldie">
    <Elements>
        <iiuga:treeelement text="All Rock" ImageIndex="0" ToolTip="AllRock">
</iiuga:treeelement>
        <iiuga:treeelement text="Classic Rock" ImageIndex="0" 
               ToolTip="ClassicRock"></iiuga:treeelement>
        <iiuga:treeelement text="Elvis Rocks!" ImageIndex="0" 
               ToolTip="ElvisRocks!"></iiuga:treeelement>
        <iiuga:treeelement text="Oldies" 
              NavigateURL="javascript:alert('Oldies');"></iiuga:treeelement>
    </Elements>
    </iiuga:treeelement>
    <iiuga:treeelement text="Classical" ToolTip="Classical" 
           CssClass="Sample3_ElementNode">
    <Elements>
        <iiuga:treeelement text="20th Century" 
                ToolTip="20thCentury"></iiuga:treeelement>
        <iiuga:treeelement text="Opera" ImageIndex="0" 
                ToolTip="Opera"></iiuga:treeelement>
        <iiuga:treeelement text="Top Classical" 
                ToolTip="TopClassical"></iiuga:treeelement>
    </Elements>
    </iiuga:treeelement>
</Elements>
</iiuga:treeweb>

 OR

C#
//
// dynamically add elements inside the tree
//

...

protected iiuga.Web.UI.TreeWeb _treeWeb;

...
    
int _elementIndex = _treeWeb.Elements.Add( "New TreeElement" );
_treeWeb.Elements[_elementIndex].CssClass = "CssClass";
_treeWeb.Elements[_elementIndex].Key = "Key";
_treeWeb.Elements[_elementIndex].ToolTip = "New TreeElement ToolTip";
    
...

... at the end

The control is far from being complete, some of its methods are not yet implemented and are nice features that can be included. But how it is now covers the normal needs of any website developer, it is a base for somebody who wants to extends its functionality and is an example for everyone who starts to develop custom server controls. For getting new versions of the TreeWeb control, for reporting bugs or for asking me any questions please feel free to go to my webpage and do it. Thanks.

Copyright

The article, as well as any accompanying source code and compiled binaries, are subject to local and international copyright laws and all rights are reserved by Iulian Iuga.

The code described in the article is available in the public domain and can be incorporated, either in its entirely or modified, into client applications. Permission is never granted for including, delivering or selling it as part of any collection of software tools.

When using or providing any of the accompanying code, please provide acknowledgment of the author. All final products must include the phrase "Portions copyright ) 2003 by Iulian Iuga" in the about screen, or where appropriate.

The source code and compiled binaries were never used in production environment and was never intended too, the author is not responsible for any damages produced by using it in that manner.

History

3 March 2003 - Submitted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThankyou! Pin
love_big_cat2-Apr-08 16:29
love_big_cat2-Apr-08 16:29 
QuestionJava Script for ExpandAll / Contract All Pin
LustForDotnet16-Jan-07 18:58
LustForDotnet16-Jan-07 18:58 
AnswerRe: Java Script for ExpandAll / Contract All Pin
prasanthikushal16-Jan-07 22:02
prasanthikushal16-Jan-07 22:02 
Aks ,

Here is JavaScript for ExpandAll/Collapse All

How to use JavaScript:
Get hold of the number of levels . Navigate to each level and set div tags visibility.
Set Button Text accordingly...

Code :
function ExpandCollapseAll()
{
var div,img;
var sTGCount = window.parent.first.treeForm.hdnArrTG.value.split("/");
try{
for( i = 0; i < parseInt(window.parent.first.treeForm.hdnTotal.value) ; i++)
{
var maindiv = '_treeWeb:element' + i;
var mainimg = '_treeWeb_element' + i + 'img';
div=document.getElementById(maindiv);
img=document.getElementById(mainimg);

if (window.parent.first.treeForm.btnExpand.value == "Expand All")
{
div.style.display = '';
img.src='./Images/ElementMinus.jpg';
}
else
{
div.style.display = 'none';
img.src='./Images/ElementPlus.jpg';
}

for( j = 0 ; j < sTGCount[i] ; j++)
{
try
{
maindiv = '_treeWeb:element' + i +':element' + j;
mainimg = '_treeWeb_element' + i + '_element' + j +'img';
div=document.getElementById(maindiv);
img=document.getElementById(mainimg);

if (window.parent.first.treeForm.btnExpand.value == "Expand All")
{
div.style.display = '';
img.src='./Images/ElementMinus.jpg';

}
else
{
div.style.display = 'none';
img.src='./Images/ElementPlus.jpg';
}
}catch(e){}
}
}
}catch(e){}
if(window.parent.first.treeForm.btnExpand.value == "Expand All")
window.parent.first.treeForm.btnExpand.value = "Collapse All";
else
window.parent.first.treeForm.btnExpand.value = "Expand All";
}

Happy Coding Smile | :)
Kusal
Questionhow to collapse or expand using javascript Pin
prasanthikushal28-Dec-06 16:57
prasanthikushal28-Dec-06 16:57 
AnswerRe: how to collapse or expand using javascript Pin
prasanthikushal16-Jan-07 18:36
prasanthikushal16-Jan-07 18:36 
GeneralRe: how to collapse or expand using javascript Pin
LustForDotnet17-Jan-07 18:48
LustForDotnet17-Jan-07 18:48 
QuestionASPNET20 Pin
andré_k29-Jun-06 7:49
andré_k29-Jun-06 7:49 
QuestionCssClass Issue Pin
shellymidha12-Jun-06 1:16
shellymidha12-Jun-06 1:16 
Questionhow to check with this tree control Pin
Khawar Abbas17-Feb-06 2:38
Khawar Abbas17-Feb-06 2:38 
QuestionUpdated TreeWeb version? Pin
JeanGael21-Oct-05 10:20
JeanGael21-Oct-05 10:20 
GeneralAutoPostBack Pin
Matteo Pietri24-Aug-05 3:38
Matteo Pietri24-Aug-05 3:38 
GeneralIsChecked state Pin
Vyjayanthi4-Aug-05 6:14
Vyjayanthi4-Aug-05 6:14 
GeneralRe: IsChecked state Pin
kendyman77731-Jul-07 4:06
kendyman77731-Jul-07 4:06 
GeneralExpand,collapse not workin when used in .ascx Pin
nikhil vaghela30-May-05 0:52
nikhil vaghela30-May-05 0:52 
GeneralRe: Expand,collapse not workin when used in .ascx Pin
Alex Fil6-Jun-05 22:37
Alex Fil6-Jun-05 22:37 
GeneralNice Job Pin
Magadass17-Apr-05 18:20
Magadass17-Apr-05 18:20 
GeneralDynamicly creating the webTree Pin
W vanEck7-Apr-05 5:39
W vanEck7-Apr-05 5:39 
GeneralRe: Dynamicly creating the webTree Pin
PaJamaMan8119-Apr-07 10:32
PaJamaMan8119-Apr-07 10:32 
GeneralCssClassOver Pin
Samuel A.C. Martins12-Mar-05 22:38
Samuel A.C. Martins12-Mar-05 22:38 
GeneralRe: CssClassOver Pin
Iulian Iuga7-Apr-05 23:48
Iulian Iuga7-Apr-05 23:48 
Generalre-rendering on expand and collapse Pin
chait20-Nov-04 22:02
chait20-Nov-04 22:02 
GeneralRe: re-rendering on expand and collapse Pin
Iulian Iuga28-Nov-04 23:17
Iulian Iuga28-Nov-04 23:17 
GeneralRe: re-rendering on expand and collapse Pin
chait3-Dec-04 13:57
chait3-Dec-04 13:57 
GeneralRe: re-rendering on expand and collapse Pin
Iulian Iuga9-Dec-04 3:28
Iulian Iuga9-Dec-04 3:28 
GeneralRe: re-rendering on expand and collapse Pin
chait9-Dec-04 20:41
chait9-Dec-04 20:41 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.