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

Implementing IHierarchy Support Into Your Custom Collections

Rate me:
Please Sign up or sign in to vote.
4.85/5 (25 votes)
17 Jul 20073 min read 157.8K   1.9K   67  
Brief walk-through on decorating your custom collections with the IHierarchy family of interfaces to support databinding.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Permissions;

namespace HierarchyExample {

	[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class CategoryDataSource : HierarchicalDataSourceControl, IHierarchicalDataSource {
		public CategoryDataSource() : base() { }

		// Return a strongly typed view for the current data source control.
		private CategoryDataSourceView view = null;
		protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath) {
			if (null == view) {
				view = new CategoryDataSourceView(viewPath);
			}
			return view;
		}

		// This can be used declaratively. To enable declarative use, 
		// override the default implementation of CreateControlCollection 
		// to return a ControlCollection that you can add to.
		protected override ControlCollection CreateControlCollection() {
			return new ControlCollection(this);
		}
	}


	public class CategoryDataSourceView : HierarchicalDataSourceView {

		private string _viewPath;
		public CategoryDataSourceView(string viewPath) {
			_viewPath = viewPath;
		}

		public override IHierarchicalEnumerable Select() {
			CategoryCollection collection = new CategoryCollection();
			foreach (Category category in Common.GetCategoryData()) {
				if (category.ParentId == 0)
					collection.Add(category);
			}

			return collection;
		}

	}

}

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
Web Developer
United States United States
Scott Piegdon works full time as a Director of Quality Control for a software development company specializing Credit Union Member Services. He has experience as a Vice President of Information Technologies for a hosted ERP (Enterprise Resource Planning) company. He also runs a Web Design Services company (http://www.simplifyitwebdesign.com). He has a beautiful and wonderful wife, and two fantastic children. He loves to develop applications that others find not only easy to use and pleasing to the eye, but also provide un-surpassing functionality. He has been developing websites for over 12 years, and programming professionally for 9 of them. He has worked with dozens of database, scripting, procedural and compiled languages over the years, most recently specializing in Microsoft.Net C# 2.0 with ASP.Net backed by SQL Server 2005.

"Hopefully I can pass along some of the insight and knowledge I have acquired over the years to those curious enough to read it." - Scott Piegdon

Comments and Discussions