A
aaronh64
I'm trying to bind to the ASP.NET TreeView control and utilize the
PopulateOnDemand functionality. However, I am receiving the following
(very confusing) error: "PopulateOnDemand only supported when binding
the TreeView to a data source control using the DataSourceID property
or when the TreeNodePopulate event is handled."
Here is the code within my page load handler:
if (!IsPostBack)
{
TreeView1.DataSource = ExamList.GetExamList();
TreeView1.DataBind();
................
ExamList.GetExamList() returns a strongly typed list (ExamList) of
items of type ExamInfo, and implements the IHierarchicalEnumerable
interface.
Here is the code within the IHierarchicalEnumerable.GetHierarchyData:
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return (IHierarchyData)enumeratedItem;
}
ExamInfo implements IHierarchyData.
Here are the important IHierarchyData member implementations:
public IHierarchicalEnumerable GetChildren()
{
return null;
}
public IHierarchyData GetParent()
{
return null;
}
public bool HasChildren
{
get { return false; }
}
public object Item
{
get { return this; }
}
On the TreeView, I am handling the TreeNodeDataBound and
TreeNodePopulate and events. They are both wired up correctly as
well.
Here is the code within my TreeNodeDataBound handler:
if (e.Node.Parent == null)
{
e.Node.SelectAction = TreeNodeSelectAction.SelectExpand;
e.Node.PopulateOnDemand = true;
e.Node.Value = ((IExam)e.Node.DataItem).ID.ToString();
}
Here is the code within my TreeNodePopulate handler:
int examID = Convert.ToInt32(e.Node.ValuePath);
Exam exam = Exam.GetExam(examID);
foreach (ExamPart part in exam.Parts)
{
TreeNode partNode = new TreeNode(part.Name);
foreach (PartItem item in part.Items)
{
TreeNode itemNode = new TreeNode(item.Name);
partNode.ChildNodes.Add(itemNode);
}
e.Node.ChildNodes.Add(partNode);
}
The misleading part of the error message is where it states "or when
the TreeNodePopulate event is handled". The event is properly wired
up, yet, the page load handler is throwing this exception immediately
after the TreeView1.DataBind(); statement is executed. In other
words, the TreeNodeDataBound event handler executes once for each
databound item, and after the last item is bound, it throws the
exception.
Any thoughts, ideas, or suggestions would be greatly appreciated!
PopulateOnDemand functionality. However, I am receiving the following
(very confusing) error: "PopulateOnDemand only supported when binding
the TreeView to a data source control using the DataSourceID property
or when the TreeNodePopulate event is handled."
Here is the code within my page load handler:
if (!IsPostBack)
{
TreeView1.DataSource = ExamList.GetExamList();
TreeView1.DataBind();
................
ExamList.GetExamList() returns a strongly typed list (ExamList) of
items of type ExamInfo, and implements the IHierarchicalEnumerable
interface.
Here is the code within the IHierarchicalEnumerable.GetHierarchyData:
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return (IHierarchyData)enumeratedItem;
}
ExamInfo implements IHierarchyData.
Here are the important IHierarchyData member implementations:
public IHierarchicalEnumerable GetChildren()
{
return null;
}
public IHierarchyData GetParent()
{
return null;
}
public bool HasChildren
{
get { return false; }
}
public object Item
{
get { return this; }
}
On the TreeView, I am handling the TreeNodeDataBound and
TreeNodePopulate and events. They are both wired up correctly as
well.
Here is the code within my TreeNodeDataBound handler:
if (e.Node.Parent == null)
{
e.Node.SelectAction = TreeNodeSelectAction.SelectExpand;
e.Node.PopulateOnDemand = true;
e.Node.Value = ((IExam)e.Node.DataItem).ID.ToString();
}
Here is the code within my TreeNodePopulate handler:
int examID = Convert.ToInt32(e.Node.ValuePath);
Exam exam = Exam.GetExam(examID);
foreach (ExamPart part in exam.Parts)
{
TreeNode partNode = new TreeNode(part.Name);
foreach (PartItem item in part.Items)
{
TreeNode itemNode = new TreeNode(item.Name);
partNode.ChildNodes.Add(itemNode);
}
e.Node.ChildNodes.Add(partNode);
}
The misleading part of the error message is where it states "or when
the TreeNodePopulate event is handled". The event is properly wired
up, yet, the page load handler is throwing this exception immediately
after the TreeView1.DataBind(); statement is executed. In other
words, the TreeNodeDataBound event handler executes once for each
databound item, and after the last item is bound, it throws the
exception.
Any thoughts, ideas, or suggestions would be greatly appreciated!