locked
Custom HierarchicalDataBoundControl RRS feed

  • Question

  • User-461574485 posted

    I have a navigation mocked up in HTML and can replicate it in a ListView Control.  What I want to do is create a HierarchicalDataBoundControl (menu, treeview) to duplicate my HTML pattern that I'm able to accomplish with the ListView.

    Does anyone have a stub that they could provide to point me in the right direction?  Or could I create a ListView control that inherits from HierarchicalDataBoundControl?

    I'm up for suggestions.

     

    Thanks!

    paul

    Wednesday, August 12, 2009 1:37 AM

Answers

  • User863160722 posted

    The simplest option is a recursive PerformDataBinding method. Something like this should work:

    public class HierarchicalBulletedList : HierarchicalDataBoundControl
    {
    	protected override void CreateChildControls()
    	{
    		if (null != Page && Page.IsPostBack && null != ViewState["_!DataBound"])
    		{
    			this.RequiresDataBinding = true;
    			this.EnsureDataBound();
    		}
    	}
    	
    	protected override void PerformDataBinding()
    	{
    		IHierarchicalEnumerable dataSource = GetData(string.Empty).Select();
    		this.PerformDataBinding(0, this.Controls, dataSource);
    		this.MarkAsDataBound();
    	}
    	
    	protected virtual void PerformDataBinding(int level, ControlCollection controls, IHierarchicalEnumerable dataSource)
    	{
    		if (null != dataSource)
    		{
    			controls.Clear();
    			
    			foreach (object value in dataSource)
    			{
    				Control item = CreateAndBindControl(level, value);
    				controls.Add(item);
    				
    				data = dataSource.GetHierarchyData(value);
    				if (null != data && data.HasChildren)
    				{
    					IHierarchicalEnumerable childData = data.GetChildren();
    					PerformDataBinding(1 + level, item.Controls, childData);
    				}
    			}
    		}
    	}
    	
    	protected virtual Control CreateAndBindControl(int level, object dataItem)
    	{
    		// Create and bind the child control here...
    	}
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 14, 2009 1:43 PM