TreeView C# ASP.NET Expand/Collapse Issues

I'm having an issue with treeview in a web application. I'm trying to generate a treeview based on a users query during run time; therefore I can't preload the treeview with any of these options.

I drag and drop a treeview object within the asp page and then I do all of the population during run time. A stripped down version of my logic is attached.

The issue I have is that in Internet Explorer the Treeview will not expand nor collapse; giving the following error:

"childNodes.0" is null or not an object.

The funny thing is that this works flawlessly in firefox. If anyone could help I'd greatly appreciate it as I've been trying to solve this for a few hours now.

To add (if it helps) if I set root.ExpandAll() I see the whole tree in Internet Explorer and in Firefox; however in Internet Explorer I can't expand or collapse it while in firefox I can.

Thanks

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%
    
        
           int MAX_RESULTS = 100;
           TreeNode[] children = new TreeNode[MAX_RESULTS];
           TreeNode[] childrenSnippets = new TreeNode[MAX_RESULTS];
           TreeNode root = new TreeNode("Google");
           googleView.Nodes.Add(root);
           for (int i = 0; i < 9; i++) 
           {
               children[i] = new TreeNode("foo"+i,"","","foo"+i,"");
               root.ChildNodes.Add(children[i]);
               childrenSnippets[i] = new TreeNode("foo"+i, "", "", "", "");
               children[i].ChildNodes.Add(childrenSnippets[i]);
               childrenSnippets[i].SelectAction = TreeNodeSelectAction.None;
               children[i].ExpandAll();
           }
       
         %>
         
        <asp:TreeView ID="googleView" runat="server">
        </asp:TreeView>
    </div>
    </form>
</body>
</html>

Open in new window

ncisupportAsked:
Who is Participating?
 
Mikal613Commented:
I have created a New object and I am not getting that error anymore.



Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
Default.aspx:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
Default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="googleView" runat="server">
        </asp:TreeView>
    </div>
    </form>
</body>
 </html>
 
//Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadTreeView();
        }
    }
    private void LoadTreeView()
    {
        int MAX_RESULTS = 100;
        TreeNode[] children = new TreeNode[MAX_RESULTS];
        TreeNode[] childrenSnippets = new TreeNode[MAX_RESULTS];
 
        TreeNode root = new TreeNode("Google");
 
        googleView.Nodes.Add(root);
 
        for (int i = 0; i < 9; i++)
        {
            root.ChildNodes.Add(new TreeNode("foo" + i, "", "", "foo" + i, ""));
            root.ChildNodes[i].ChildNodes.Add(new TreeNode("foo" + i, "", "", "", ""));
            root.ChildNodes[i].SelectAction = TreeNodeSelectAction.None;
            if (root.ChildNodes[i].ChildNodes.Count > 0)
            {
                root.ChildNodes[i].ExpandAll();
            }
        }
    }
}

Open in new window

0
 
GiftsonDJohnCommented:
Hi

Try this instead.
           TreeNode root = new TreeNode("Google");
           googleView.Nodes.Add(root);
           for (int i = 0; i < 9; i++) 
           {
               TreeNode nodeA = new TreeNode("foo"+i,"","","foo"+i,"");
               nodeA.ID="foo" + i;
               root.ChildNodes.Add(nodeA);
               TreeNode nodeB = new TreeNode("foo"+i, "", "", "", "");
               nodeB.ID=nodeA.ID + i;
               nodeA.ChildNodes.Add(nodeB);
               nodeB.SelectAction = TreeNodeSelectAction.None;
               nodeA.ExpandAll();
           }

Open in new window

0
 
Mikal613Commented:
Instead of putting the code in the aspx file, put it in the Code behind. (.cs)

//Code It works with this

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadTreeView();
        }
    }
    private void LoadTreeView()
    {
        int MAX_RESULTS = 100;
        TreeNode[] children = new TreeNode[MAX_RESULTS];
        TreeNode[] childrenSnippets = new TreeNode[MAX_RESULTS];

        TreeNode root = new TreeNode("Google");

        googleView.Nodes.Add(root);

        for (int i = 0; i < 9; i++)
        {
            root.ChildNodes.Add(new TreeNode("foo" + i, "", "", "foo" + i, ""));
            root.ChildNodes[i].ChildNodes.Add(new TreeNode("foo" + i, "", "", "", ""));
            root.ChildNodes[i].SelectAction = TreeNodeSelectAction.None;
            if (root.ChildNodes[i].ChildNodes.Count > 0)
            {
                root.ChildNodes[i].ExpandAll();
            }
        }
    }
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
ncisupportAuthor Commented:
GiftsonDJohn: The TreeNode objects I'm manipulating do not have an ID field. I'm coding this in WebDev 2008 .NET 3.5

Mikal613: I've taken that suggestion; thank you. However, this still does not resolve my issue.

If it helps, I've noticed that when I look at the value path of the childrenSnippets they show 'Google//' while the children show 'Google/' - seems to me as if the structure is not correct?
0
 
Mikal613Commented:
Did you delete the code from your ASPx page?

your aspx page should look like

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
        <asp:TreeView ID="googleView" runat="server">
        </asp:TreeView>
    </div>
    </form>
</body>
</html>
0
 
ncisupportAuthor Commented:
Mikal613: Yes, I deleted the other code from my ASP page. I also went as far as re-creating my scenario in a new project and testing; same results.

My exact error in IE is:

'childNodes.0' is null or not an object.
0
 
Mikal613Commented:
is your website AJAX enabled?
0
 
ncisupportAuthor Commented:
Mikal613: I can confirm that this is now working; yes. However, I still get the same issue in my active project.

Perhaps it is my logic? This is my process:
1) User types query hits search
2) search is in codebehind and it calls the google api
3) google api returns google api result datatype
4) this result is then stored in a custom struct and the google api result datatype is free'd
5) struct is passed to another method that will populate the tree

All of this is happening on the same page; this shouldn't be an issue should it?

The way I invoke the searching logic is an if else statement in my aspx (not codebehind) that says if the button was clicked and google was checked then call the google api via the google method in the codebehind.

Any ideas?

Been a great help thank you.
0
 
Mikal613Commented:
All of this is happening on the same page; this shouldn't be an issue should it?
<< no it shouldnt

do you want to post some code?

0
 
ncisupportAuthor Commented:
Mikal613:

I've narrowed it down to this:

It works if I invoke the LoadTreeView method from the PageLoad()

If I invoke it from something else then it does not work.

Is PageLoad invoked every time the page has a change (i.e. button clicked) or only the first time the user loads it?
0
 
Mikal613Commented:
Notice the IsPostBAck flag


On the First load is IsPostBack flag is false

Any time there is a postback it will be true
0
 
ncisupportAuthor Commented:
Mikal613: Fully resolved. Thank you quite kindly!
0
 
ncisupportAuthor Commented:
Thank again!
0
 
Mikal613Commented:
no problem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.