Home - Forums-.NET - FlyTreeView (ASP.NET) - OnPopulateNodes can work first time only!

FlyTreeView (ASP.NET)

Technical support and KB related to the FlyTreeView control

This forum related to following products: FlyTreeView for ASP.NET

OnPopulateNodes can work first time only!
Link Posted: 14-Jun-2007 04:37
imitate the sample,but use MS sqlserver

    private void AddNodes(NineRays.WebControls.FlyTreeNodeCollection flyTreeNodeCollection, string id)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[\"MyConnectionString1\"].ToString());
        SqlDataAdapter da = new SqlDataAdapter(\"select * from drx_department where parent_id=\" + id, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            FlyTreeNode ftn = new FlyTreeNode();
            ftn.Text = row[\"department_name\"].ToString();
            ftn.Value = row[\"department_id\"].ToString();
            ftn.PopulateNodesOnDemand = true;
            try
            {
                flyTreeNodeCollection.Add(ftn);
            }
            catch (Exception ex)
            {
                // add a child node that shows that there was an exception trying to get its child nodes
                AddExceptionNode(ftn.ChildNodes, ex);
            }
        }
    }

Click one node of root after page load,OnPopulateNodes is executed and the ChildNodes can be displayed in good state.But click one of these ChildNodes,show \"loading\" at all times.
What's wrang with my code?Thanks
Link Posted: 14-Jun-2007 08:24
How is your AddNodes method called from PopulateNode handler?

Also, do you get any exception during callback when debugging the page?

I also do not understand why you have try-catch block since flyTreeNodeCollection.Add(ftn) typically should not raise any exceptions.
Link Posted: 14-Jun-2007 15:18
[quote="EvgenyT"]How is your AddNodes method called from PopulateNode handler? Also, do you get any exception during callback when debugging the page? I also do not understand why you have try-catch block since flyTreeNodeCollection.Add(ftn) typically should not raise any exceptions.
Thanks for your response. my complete code: [code] <%@ Register Assembly="NineRays.WebControls.FlyTreeView" Namespace="NineRays.WebControls" TagPrefix="NineRays" %> test
[/code]
Link Posted: 14-Jun-2007 15:23

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using NineRays.WebControls;

public partial class _Defult: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AddNodes(flyTreeView.Nodes, \"1\");
        }
    }

    protected void flyTreeView_PopulateNodes(object sender, NineRays.WebControls.FlyTreeNodeEventArgs e)
    {
        AddNodes(e.Node.ChildNodes, e.Node.Value);
    }
    private void AddNodes(NineRays.WebControls.FlyTreeNodeCollection flyTreeNodeCollection, string id)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[\"MyConnectionString1\"].ToString());
        SqlDataAdapter da = new SqlDataAdapter(\"select * from drx_department where parent_id=\" + id, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            FlyTreeNode ftn = new FlyTreeNode();
            ftn.Text = row[\"department_name\"].ToString();
            ftn.Value = row[\"department_id\"].ToString();
            ftn.PopulateNodesOnDemand = true;
            try
            {
                flyTreeNodeCollection.Add(ftn);
            }
            catch (Exception ex)
            {
                // add a child node that shows that there was an exception trying to get its child nodes
                AddExceptionNode(ftn.ChildNodes, ex);
            }
            //using recursion the program runs well
            //AddNodes(ftn.ChildNodes, ftn.Value);
        }
    }
    
    private void AddExceptionNode(FlyTreeNodeCollection nodes, Exception ex)
    {
        FlyTreeNode ftn = new FlyTreeNode();
        ftn.Text = HttpUtility.HtmlEncode(ex.Message);
        ftn.NodeTypeID = \"errorType\";
        nodes.Add(ftn);
    }
}


I set a break point in AddNodes function.It pauses When Page_Load and fireing PopulateNodes event in first level.No pause in next level and no exception.
I am very puzzled...
Link Posted: 14-Jun-2007 22:12
I've just copied your code and changed the only datatable populating part:

private void AddNodes(NineRays.WebControls.FlyTreeNodeCollection flyTreeNodeCollection, string id)
{
    // create sample datatable
    DataTable dt = new DataTable();
    dt.Columns.Add(\"department_name\");
    dt.Columns.Add(\"department_id\");
    dt.Rows.Add(\"node 1\", \"1\");
    dt.Rows.Add(\"node 2\", \"2\");
    dt.Rows.Add(\"node 3\", \"3\");
    dt.Rows.Add(\"node 4\", \"4\");
    foreach (DataRow row in dt.Rows)
    {
        FlyTreeNode ftn = new FlyTreeNode();
        ftn.Text = row[\"department_name\"].ToString();
        ftn.Value = row[\"department_id\"].ToString();
        ftn.PopulateNodesOnDemand = true;
        try
        {
            flyTreeNodeCollection.Add(ftn);
        }
        catch (Exception ex)
        {
            // add a child node that shows that there was an exception trying to get its child nodes
            AddExceptionNode(ftn.ChildNodes, ex);
        }
        //using recursion the program runs well
        //AddNodes(ftn.ChildNodes, ftn.Value);
    }
}



And everything works OK.
So I think you should check what you have in flyTreeNodeCollection in the end of AddNodes method.
Link Posted: 14-Jun-2007 22:16
Also, do you have a valid HTML in department_name?

Try to change
ftn.Text = row[\"department_name\"].ToString();


to

ftn.Text = Server.HtmlEncode(row[\"department_name\"].ToString());
Link Posted: 15-Jun-2007 01:06
Thanks again for support.
I think I got the reason.
If FlyTreeNode.Text or FlyTreeNode.Value include non-english characters,The program failed to populate In most cases.
Try this code:

private void AddNodes(NineRays.WebControls.FlyTreeNodeCollection flyTreeNodeCollection, string id)
{
    // create sample datatable
    DataTable dt = new DataTable();
    dt.Columns.Add(\"department_name\");
    dt.Columns.Add(\"department_id\");
    dt.Rows.Add(\"啊\", \"1\");
    dt.Rows.Add(\"node 2\", \"2\");
    dt.Rows.Add(\"node 3\", \"3\");
    dt.Rows.Add(\"node 4\", \"4\");
    foreach (DataRow row in dt.Rows)
    {
        FlyTreeNode ftn = new FlyTreeNode();
        ftn.Text = row[\"department_name\"].ToString();
        ftn.Value = row[\"department_id\"].ToString();
        ftn.PopulateNodesOnDemand = true;
        try
        {
            flyTreeNodeCollection.Add(ftn);
        }
        catch (Exception ex)
        {
            // add a child node that shows that there was an exception trying to get its child nodes
            AddExceptionNode(ftn.ChildNodes, ex);
        }
        //using recursion the program runs well
        //AddNodes(ftn.ChildNodes, ftn.Value);
    }
}
Link Posted: 15-Jun-2007 04:42
Works ok at my place in IE and Firefox.

Here what I get:


What request/response encoding do you use for your web application?
Link Posted: 15-Jun-2007 08:17
Great!
It is about encoding actually.I modifyed the web.config.It works good.
Thanks a loooot.