Solved

TreeView C# ASP.NET Expand/Collapse Issues

Posted on 2009-05-04
14
2,252 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:ncisupport
  • 7
  • 6
14 Comments
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24304039
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 24304213
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
 

Author Comment

by:ncisupport
ID: 24304780
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 24304819
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
 

Author Comment

by:ncisupport
ID: 24305173
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 24305448
is your website AJAX enabled?
0
 
LVL 48

Accepted Solution

by:
Mikal613 earned 500 total points
ID: 24305518
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ncisupport
ID: 24306348
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 24306454
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
 

Author Comment

by:ncisupport
ID: 24307404
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 24307451
Notice the IsPostBAck flag


On the First load is IsPostBack flag is false

Any time there is a postback it will be true
0
 

Author Comment

by:ncisupport
ID: 24307729
Mikal613: Fully resolved. Thank you quite kindly!
0
 

Author Closing Comment

by:ncisupport
ID: 31577733
Thank again!
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 24307759
no problem
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now