如何在TreeView中捕获更改的“选择/未选择”复选框节点

时间:2019-02-14 01:54:06

标签: c# asp.net

我有一个asp Treeview,供我理解。    我想捕获用户选择的所有选中和取消选中复选框。

我尝试使用按钮单击事件来检查CheckNodes。但是我想知道如何在单击“保存”按钮之前获取“未选定”节点。

根据代码, 这是我的树状视图的格式 在“页面加载”中,仅检查3个节点。

如果用户取消选择几个节点并按保存按钮,该怎么办。

我怎么知道在回发时捕获已选中/未选中复选框的信息。

enter image description here

用户操作

  1. 选择-Head1Child1GrandChild1
  2. 取消选择-Head1Child2 &&              Head1Child3

  3. 预期结果:需要捕获复选框   选择-Head1Child1 && Head1Child1GrandChild1(Save_Click中的代码)

    并且还需要捕获

    取消选择-Head1Child2 &&                Head1Child3

给定的树形视图只是为我理解问题的原型。想象一下,如果有1000个节点,并且用户选择了某个节点,然后取消选择了所选节点。我只想仅获取那些未选择和选择的节点。就是这样。

enter image description here


谢谢您的时间。

<body>
    <form id="form1" runat="server">
        <div>
            <asp:TreeView ID="TreeView1" ShowCheckBoxes="All" Showlines="true" runat="server"></asp:TreeView>
            <asp:Button ID="Button1" runat="server" OnClick="Save_Click" Text="Save" />
            <br />
        </div>
    </form>
</body>


    //CS Code
    //TreeView on a PageLoad. 
    //It will create TreeView with all Checkbox checked
    // If the user Unselects one check box how do i capture that in a event ? 



  protected void Page_Load(object sender, EventArgs e)
            {
              if(!IsPostback)
{
                TreeNode tNode1 = new TreeNode();
                tNode1.Text = "HeadNode1";
                tNode1.Value = "HeadNode1";

                TreeNode h1ChildNode1 = new TreeNode();
                h1ChildNode1.Text = "Head1Child1";
                tNode1.ChildNodes.Add(h1ChildNode1);

                TreeNode h1GrandChild1 = new TreeNode();
                h1GrandChild1.Text = "Head1Child1Grand1";
                h1ChildNode1.ChildNodes.Add(h1GrandChild1);

                TreeNode h1ChildNode2 = new TreeNode();
                h1ChildNode2.Text = "Head1Child2";
                tNode1.ChildNodes.Add(h1ChildNode2);

                TreeNode h1ChildNode3 = new TreeNode();
                h1ChildNode3.Text = "Head1Child3";
                tNode1.ChildNodes.Add(h1ChildNode3);
                TreeView1.Nodes.Add(tNode1);

                TreeNode tNode2 = new TreeNode();
                tNode2.Text = "HeadNode2";
                tNode2.Value = "HeadNode2";


                TreeView1.Nodes.Add(tNode2);

                ServerSideChangeSelection(TreeView1, true);

            }

           protected TreeView ServerSideChangeSelection(TreeView t, bool check)
        {
            foreach (TreeNode tn in t.Nodes)
            {
                tn.Checked = false;
                if (tn.ChildNodes.Count > 0)
                {
                    foreach (TreeNode childNd in tn.ChildNodes)
                    {
                        childNd.Checked = check;

                    }                    
                }
            }
            return t;
        }
}

//保存按钮单击CheckNodes。缺少取消选择的节点??

            protected void Save_Click(object sender, EventArgs e)
            {
                List<string> checkNodes = new List<string>();
                List<string> unCheckNodes = new List<string>();


                foreach (var item in TreeView1.CheckedNodes)
                {

                    checkNodes.Add(item.ToString());
                 }
            }

1 个答案:

答案 0 :(得分:1)

我将对您的代码进行一些更改:

创建了如下所示的属性:

public List<TreeNode> Nodes
{
    get
    {
        if (HttpContext.Current.Session["Nodes"] == null)
        {
            HttpContext.Current.Session["Nodes"] = new List<TreeNode>();
        }
        return HttpContext.Current.Session["Nodes"] as List<TreeNode>;
    }
    set
    {
        HttpContext.Current.Session["Nodes"] = value;
    }
}

在您的page_load中:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TreeNode tNode1 = new TreeNode();
        tNode1.Text = "HeadNode1";
        tNode1.Value = "HeadNode1";

        TreeNode h1ChildNode1 = new TreeNode();
        h1ChildNode1.Text = "Head1Child1";
        tNode1.ChildNodes.Add(h1ChildNode1);

        TreeNode h1GrandChild1 = new TreeNode();
        h1GrandChild1.Text = "Head1Child1Grand1";
        h1ChildNode1.ChildNodes.Add(h1GrandChild1);

        TreeNode h1ChildNode2 = new TreeNode();
        h1ChildNode2.Text = "Head1Child2";
        tNode1.ChildNodes.Add(h1ChildNode2);

        TreeNode h1ChildNode3 = new TreeNode();
        h1ChildNode3.Text = "Head1Child3";
        tNode1.ChildNodes.Add(h1ChildNode3);
        TreeView1.Nodes.Add(tNode1);

        TreeNode tNode2 = new TreeNode();
        tNode2.Text = "HeadNode2";
        tNode2.Value = "HeadNode2";

        TreeView1.Nodes.Add(tNode2);

        ServerSideChangeSelection(TreeView1, true);
        List<TreeNode> nodes = new List<TreeNode>();

        foreach (TreeNode node in TreeView1.Nodes)
        {
            nodes.Add(node);
            if (node.ChildNodes.Count > 0)
            {
                foreach (TreeNode childNode in node.ChildNodes)
                {
                    nodes.Add(childNode);
                }
            }
        }

        Nodes = nodes;
    }
}

保存_点击

protected void Save_Click(object sender, EventArgs e)
{
    List<TreeNode> unCheckNodes = new List<TreeNode>();

    GetCheckUncheckTreeNodes(TreeView1.Nodes, ref unCheckNodes);
}

private void GetCheckUncheckTreeNodes(TreeNodeCollection nodeCollection, ref List<TreeNode> unCheckNodes)
{
    if (Nodes != null)
    {
        foreach (TreeNode node in nodeCollection)
        {
            if (!node.Checked && Nodes.Any(x => x.Text == node.Text && x.Checked != node.Checked))
                unCheckNodes.Add(node);

            if (node.ChildNodes.Count > 0)
                GetCheckUncheckTreeNodes(node.ChildNodes, ref unCheckNodes);
        }
    }
}