ASP.NET 2.0 TreeView Binding Issue

Discussion in 'ASP .Net' started by aaronh64@hotmail.com, Feb 12, 2007.

  1. Guest

    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!
     
    , Feb 12, 2007
    #1
    1. Advertising

  2. J'son Guest

    Arron,

    Did you ever figure this one out? I'm having the exact same problem..

    Thanx!

    J'son

    On Feb 12, 12:35 pm, "" <>
    wrote:
    > 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 supportedwhen 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!
     
    J'son, Apr 9, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Banda RamaNarsi Reddy

    Binding Treeview in datagrid(ASP.Net(C#))

    Banda RamaNarsi Reddy, Mar 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    4,844
    Banda RamaNarsi Reddy
    Mar 10, 2005
  2. Ken Fine
    Replies:
    0
    Views:
    511
    Ken Fine
    May 17, 2004
  3. Banda RamaNarsi Reddy

    Binding Treeview in datagrid(ASP.Net(C#))

    Banda RamaNarsi Reddy, Mar 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    695
    Banda RamaNarsi Reddy
    Mar 10, 2005
  4. Jeff Gao

    asp.net Treeview data binding update

    Jeff Gao, Mar 6, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    2,137
    Jeff Gao
    Mar 6, 2008
  5. Ionutz
    Replies:
    2
    Views:
    568
    Ionutz
    Oct 23, 2005
Loading...

Share This Page