Adding TreeView nodes at runtime, but Node count stays at 1

Hi,

I have a treeview control that populates from a database.  I hardcoded a root node, and using PopulateOnDemand I believe this attempts to populate the TreeView when opening the page

The treeview populates OK, without a problem.  However the issue is coming where I want to add another node to one of the nodes added at run time.  It is only counting 1 node (so therefore, I cannot iterate through all the nodes to find the one I'm adding to, as the program believes there is only one node!)


<!--aspx file-->
<asp:TreeView ID="tvTariff" runat="server" OnSelectedNodeChanged="tvTariff_SelectedNodeChanged" OnTreeNodePopulate="tvTariff_TreeNodePopulate" ShowLines="True">
    <Nodes>
        <asp:TreeNode PopulateOnDemand="True" Text="Tariff List" Value="Tariff List"></asp:TreeNode>
    </Nodes>
</asp:TreeView>
 
 
//C#
protected void tvTariff_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
    if (e.Node.ChildNodes.Count == 0)
    {
        switch (e.Node.Depth)
        {
            case 0:
                //Tariff - root
                PopulateParent(e.Node);
                break;
            case 1:
                //Add Code Group - tariff clicked on
                PopulateCodeGroup(e.Node);
                break;
        }
    }
}
 
void PopulateParent(TreeNode node)
{
    //TreeViewSrc is a class file that returns the data from the database
    TreeViewSrc trSrc = new TreeViewSrc();
    DataTable dtRoot = new DataTable("Tariff");
 
    dtRoot = trSrc.RootNodes();
 
    foreach (DataRow row in dtRoot.Rows)
    {
        TreeNode NewNode = new TreeNode(row["ta_name"].ToString(), row["ta_id"].ToString());
        NewNode.PopulateOnDemand = true;
        NewNode.SelectAction = TreeNodeSelectAction.SelectExpand;
        node.ChildNodes.Add(NewNode);
 
    }
}
 
 
//Code that finds the node, and adds a new child - this is the bit not working, as it only counts 1 node
protected void btnAddCG_Click(object sender, EventArgs e)
{
    int i;
    i = tvTariff.Nodes.Count;
 
    for(i=0;i < tvTariff.Nodes.Count;i++)
    {
        switch (tvTariff.Nodes[i].Depth)
        {
            case 1:
                //only looking for tariff node to add a session node to
                CheckTariffNode(tvTariff.Nodes[i]);
                break;
        }
    }
}
protected void CheckTariffNode(TreeNode node)
{
    if (node.Value==Session["SelectedNode"].ToString())
    {
        TreeNode NewNode = new TreeNode(dlTariffCG.SelectedItem.ToString(),dlTariffCG.SelectedValue.ToString());
        NewNode.PopulateOnDemand = true;
        NewNode.SelectAction = TreeNodeSelectAction.SelectExpand;
 
        node.ChildNodes.Add(NewNode);
    }
}

Open in new window

LVL 1
Member_4438002Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Member_4438002Author Commented:
This is now resolved.

In the tvTariff_TreeNodePopulate routine, I just put after the if statement :

this.ParseControl("tvTariff");

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.