Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a Treeview control on a webpage. There are over 1900 nodes at the lowest level so I am trying to use PopulateOnDemand. This works well until I want to find all of the nodes that the user has selected.

When any of the nodes that are initially populated are checked, they are included in the CheckedNodes property of the Treeview when I click the Order Selected Sections button.

If I navigate to a level that is not initially populated, tvDocList_TreeNodePopulate is executed and the node is filled with the correct information. If I now check the box next to one of the newly populated nodes and click the Order Selected Sections button, CheckedNodes does not contain any of the nodes that were populated on demand.

I have tried to get the checked nodes by using TreeNodeCheckChanged, but the only nodes that come through are the ones that are initially loaded. Any of the ones loaded on demand are ignored.

Any suggestions?
Thanks in advance.

Here is the code I am using.

XML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ShoppingCart._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  <h2>
        Welcome to Great River Printing!
    </h2>
    <asp:Table ID="Table1" runat="server" Width="100%">
    <asp:TableRow runat="server">
      <asp:TableCell runat="server">
        <asp:Button ID="btnOrderSelected" runat="server" Text="Order Selected Sections" />
      
    
  
    <asp:TreeView ID="tvDocList" runat="server" Width="316px" ShowCheckBoxes="All">
    <nodes>
      <asp:TreeNode ShowCheckBox="True" Text="Great River" Value="Great River">
      
    </nodes>


C#
public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      LoadPage();
    }
    btnOrderSelected.Click +=
      new EventHandler(btnOrderSelected_Click);
    tvDocList.TreeNodePopulate +=
      new TreeNodeEventHandler(tvDocList_TreeNodePopulate);
    tvDocList.TreeNodeCheckChanged +=
      new TreeNodeEventHandler(tvDocList_TreeNodeCheckChanged);
  }

  protected void LoadPage()
  {
    tvDocList.Nodes.Clear();
    tvDocList.ExpandDepth = 3;
    foreach (PageProject project in MySession.Current.Projects)
    {
      TreeNodeData node = project.AddToTree(null);
      node.AddToTree(tvDocList.Nodes);
    }
  }

  private void btnOrderSelected_Click(object sender, EventArgs e)
  {
    List<int> checkedIds = new List<int>();
    int id;

    foreach (TreeNode tn in tvDocList.CheckedNodes)
    {
      if (int.TryParse(tn.Value, out id))
        checkedIds.Add(id);
    }
  }

  void tvDocList_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
  {
    PageSection p = GetPageSection(e);
  }

  void tvDocList_TreeNodePopulate(object sender, TreeNodeEventArgs e)
  {
    PageSection pageSection = GetPageSection(e);
    if (pageSection != null)
    {
      TreeNodeData tnd = pageSection.AddToTree(null);
      tnd.AddChildrenToTree(e.Node);
    }
  }

  private PageSection GetPageSection(TreeNodeEventArgs e)
  {
    PageSection pageSection = null;
    int id;
    if (int.TryParse(e.Node.Value, out id))
    {
      foreach (PageProject project in MySession.Current.Projects)
      {
        pageSection = project.FindPageSection(id);
        if (pageSection != null)
        {
          pageSection.Selected = e.Node.Checked;
          break;
        }
      }
    }
    return pageSection;
  }
}
</int></int>
Posted
Updated 11-Jun-18 8:56am
v2

1 solution

An old question. But, since this kept showing in search results when I too was researching the issue you describe, I thought I would post a solution for others that may come across this.

When doing TreeView control on a web page that uses PopulateOnDemand, You basically want to start here: [^]

For the checkboxes, however, if you want all the child nodes to be checked when the parent node is checked, you're going to need JavaScript. For that, I found this to work well: c# - Treeview in web form check child nodes on parent node check - Stack Overflow[^]

The only piece missing from that, is the situation you describe. For example, if a node has not yet been expanded, and therefore populated, then there are no checkboxes yet to be checked. So if the parent node is checked when the node is expanded, none of the children will be checked.

The fix for that, as it turns out, is simple. In the routine that adds a child node, and directly before adding the child node, then include the following:

//  If the node that we are expanding and populating is checked, then check all the children.
if (node.Checked) {
     newNode.Checked = true;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900