Click here to Skip to main content
15,881,248 members
Articles / Web Development / HTML

DHTML Tree View of Arbitrary Depth using AJAX

Rate me:
Please Sign up or sign in to vote.
4.60/5 (24 votes)
25 Aug 20054 min read 186.9K   2.1K   84  
This article provides a gentle introduction to AJAX by applying that technology to significantly enhance a tree previously rendered using JavaScript.
// Copyright (c)2005 Rewritten Software.  http://www.rewrittensoftware.com
// This script is supplied "as is" without any form of warranty. Rewritten Software 
// shall not be liable for any loss or damage to person or property as a result of using this script.
// Use this script at your own risk!
// You are licensed to use this script free of charge for commercial or non-commercial use providing you do not remove 
// the copyright notice or disclaimer.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
	/// <summary>
	/// Responder for child nodes
	/// </summary>
	public class TreeFill : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		/// <summary>
		/// Render the nodes attached to a node at a specific level in the tree
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer)
		{
			string treeID = Request["tree"];
			if (treeID == null)
				throw new HttpException("Tree identifier not found in request during delay load.");

			Tree tree = (Tree)Session["tree" + treeID];
			if (tree == null)
				throw new HttpException("Tree was not found in session during delay load.");

			string nodeID = Request["id"];
			if (nodeID == null)
				throw new HttpException("Node id null in delay load.");
        
			// Locate and output the requested node
			bool found = false;
			OutputTreeNode(nodeID, treeID, writer, tree.Root, ref found);
		}
    
		/// <summary>
		/// Recursive: Search the tree for the relevant node and output its children
		/// </summary>
		/// <param name="nodeID">parent node id</param>
		/// <param name="treeID">id of this tree</param>
		/// <param name="writer">output writer</param>
		/// <param name="node">starting node</param>
		/// <param name="found">found yet or not</param>
		void OutputTreeNode(string nodeID, string treeID, HtmlTextWriter writer, TreeNode node, ref bool found)
		{
			// We found the node, output its children, and stop.
			if (nodeID == node.ID)
			{
				foreach (TreeNode child in node.Children)
					child.WriteNode(writer, treeID, true);
				found = true;
				return;
			}

			// Not found yet, search children for the node.
			if (!found)
				foreach (TreeNode child in node.Children)
					OutputTreeNode(nodeID, treeID, writer, child, ref found);
		}

		#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.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}

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 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
Architect Webefinity
Australia Australia
Adrian is current the Solution Architect at CubeBuild.com.

The core of CubeBuild is a website and application platform that is pluggable into ASP.NET MVC. Any MVC application can have content authoring added to its pages with little effort, and new content types are created using IronPython.NET open source components.

We are currently deploying a Point of Service (Web based POS) built on CubeBuild which allows a single web channel for face-to-face sales, and sales through your online store. All from a single inventory base, and from any device.

Comments and Discussions