Click here to Skip to main content
15,884,353 members
Articles / Web Development / ASP.NET

Creating a highly manageable TreeView control using Microsoft Web TreeView control

Rate me:
Please Sign up or sign in to vote.
4.46/5 (15 votes)
20 Sep 2005CPOL4 min read 144.1K   3.7K   62  
An atricle on how to implement a manageable treeview architecture.
namespace TreeView.Design.Controls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Xml;

	using TreeView.Design.Base;
	/// <summary>
	///		Summary description for Company.
	/// </summary>
	public class Company : BaseControl, IHost
	{
		protected System.Web.UI.WebControls.TextBox txtCompany;
		protected System.Web.UI.WebControls.Button btnAdd;
		protected ObjectDump companyDump;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code

		
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region IHost Members

	
		public void LoadTab(int PropertyID)
		{
			XmlDocument doc = new XmlDocument();
			doc.Load( Server.MapPath( "Data.xml" ) );

			_CompanyID = PropertyID;
			
			XmlNodeList nodeList = doc.GetElementsByTagName( "company" );
			
			foreach ( XmlNode node in nodeList )
			{
				int nodeID = Convert.ToInt32(  node.Attributes[ "id" ].Value );
				
				if( nodeID == PropertyID )
				{
					string companyName = node.Attributes[ "name" ].Value;

					Objects.Company company = new Objects.Company( nodeID, companyName );
					// load the object
					companyDump.LoadObject( company );
				}
			}
		}
		private int _CompanyID
		{
			get
			{
				return Convert.ToInt32(  base.ViewState[ "CompanyID" ] );
			}
			set
			{
				base.ViewState[ "CompanyID" ] = value;
			}
		}

		#endregion

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			AddNode( Server.MapPath(  "Data.xml" ) );			
			// notify observer ( in this case treeView )
			base.RaiseChangeEvent( true );
		
		}

		private void AddNode( string docPath )
		{
			XmlDocument doc = new XmlDocument();
			doc.Load( docPath );
			
			XmlNodeList nodeList = doc.GetElementsByTagName( "company" );
		
			foreach( XmlNode node in nodeList )
			{
	
				//XmlNode node = nodeList [ nodeList.Count - 1 ];

				int nodeID = Convert.ToInt32(  node.Attributes[ "id" ].Value );

				if( _CompanyID == nodeID )
				{
					
					XmlNode childNode = node.ChildNodes [ node.ChildNodes.Count -1 ];
				   
					int childID = Convert.ToInt32( 	childNode.Attributes[ "id" ].Value );

					int newID = ++childID;

					XmlElement deptElement = doc.CreateElement( "department" );
				
					// id attribute
					deptElement.SetAttribute( "id",newID.ToString() ) ;
					deptElement.SetAttribute( "name",txtCompany.Text.Trim() ) ;
				

					node.AppendChild( deptElement );
			
					string s = doc.InnerXml;
					
					doc.Save( docPath );

				}
			}// end foreach
	
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Telerik Corporation
Bangladesh Bangladesh
Passionate about cutting edge technologies and a .net enthusiast. I have played roles in variety of products starting from University automation to web 2.0 start-page (www.pageflakes.com).

Currently, working at Telerik Inc (www.telerik.com), the premium rad control provider for asp.net and winforms. I am a active contributor and member at www.dotnetslackers.com. In addition, i do a frequent post on my blog about LINQ, C#, Asp.net and about my projects.

I am Microsoft MVP. I love to travel and meet cool people.

Comments and Discussions