使用PopulateOnDemand和Checkbox的ASP.NET Treeview无法正常工作 [英] ASP.NET Treeview using PopulateOnDemand and Checkboxes not working as needed

查看:71
本文介绍了使用PopulateOnDemand和Checkbox的ASP.NET Treeview无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页上使用Treeview控件。最低级别有超过1900个节点,因此我尝试使用PopulateOnDemand。这很有效,直到我想找到用户选择的所有节点。



当检查最初填充的任何节点时,它们包含在单击Order Selected Sections按钮时Treeview的CheckedNodes属性。



如果我导航到最初未填充的级别,则执行tvDocList_TreeNodePopulate并且节点为充满了正确的信息。如果我现在选中其中一个新填充的节点旁边的框并单击Order Selected Sections按钮,则CheckedNodes不包含按需填充的任何节点。



我试图通过使用TreeNodeCheckChanged来获取已检查的节点,但是唯一通过的节点是最初加载的节点。任何按需加载的都会被忽略。



有什么建议吗?

提前致谢。



这是我正在使用的代码。



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.

<%@ 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>







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>

推荐答案

一个老问题。但是,因为当我也在研究你描述的问题时,搜索结果会一直显示,我想我会为其他可能会遇到此问题的人发布一个解决方案。



在使用PopulateOnDemand的网页上执行TreeView控件时,您基本上想从这里开始: [ ^ ]



但是,对于复选框,如果您希望在选中父节点时检查所有子节点,您将需要JavaScript。为此,我发现这很好用: c# - Web视图中的Treeview检查父节点上的子节点检查 - Stack Overflow [ ^ ]



唯一丢失的一块,是你描述的情况。例如,如果某个节点尚未展开,因此已填充,则表示尚未检查任何复选框。因此,如果在扩展节点时检查父节点,则不会检查任何子节点。



事实证明,对此的修复很简单。在添加子节点的例程中,直接在添加子节点之前,请包含以下内容:



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;
}


这篇关于使用PopulateOnDemand和Checkbox的ASP.NET Treeview无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆