在树视图中更改节点颜色 [英] Change node color in treeview

查看:127
本文介绍了在树视图中更改节点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个带有nodes的树视图。我正在通过树视图节点进行搜索。一旦找到匹配节点,就应该更改节点颜色。这样工作正常,但问题是在搜索新节点时,先前搜索的节点颜色也被改变。任何帮助将非常感谢。谢谢。



我尝试过:



Hi All,
I have a treeview with nodes.I am implementing searching through treeview nodes .Once matching node is find ,the node color should be changed .it is working fine ,but the problem is while searching for a new node the previously searched node color is also changed . Any help will be really appreciated .Thanks in advance.

What I have tried:

protected void btn_search_Click(object sender, EventArgs e)
       {
           try
           {
               FindNodesByString();

           }

           catch { }
       }

       private void FindNodesByString()
       {
           foreach (TreeNode currentNode in tv_AccountView.Nodes)
           {
               FindNodeByString(currentNode);
           }
       }

       private void FindNodeByString(TreeNode parentNode)
       {
           FindMatch(parentNode);
           foreach (TreeNode currentNode in parentNode.ChildNodes)
           {
               //currentNode.Text = currentNode.Text;
               FindMatch(currentNode);
               FindNodeByString(currentNode);
           }
       }
      private void FindMatch(TreeNode currentNode)
       {
           if (currentNode.Text.ToUpper().Contains(txt_searchbyname.Text.ToUpper()))
           {
               currentNode.Expand();
               currentNode.Text = "<div style='background-color:#ffffcc;color:#ff9900;'>" + currentNode.Text + "</div>";

   /// currentNode.ShowCheckBox = true;
               //return;
           }
           else
           {
               currentNode.Text = currentNode.Text;
               // currentNode.Collapse();
             //  currentNode.ShowCheckBox = false;
           }
       }

推荐答案

那是因为你用<$ c设置颜色$ c> div 元素。一旦在客户端/浏览器中呈现它,它将保留在那里。您的 else 代码块仍将保留文本,其中包含< div> 元素。你需要实现一些文本解析来解决这个问题。



这是一个快速演示:



ASPX:

That's because you are setting the color with a div element. Once that's been rendered in the client/browser, it will remain there. Your else block of your code will still hold the Text with the <div> element to it. You would need to implement a bit of text parsing to resolve that.

Here's a quick demo:

ASPX:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TreeView ID="TreeView1" runat="server">
            <Nodes>
                <asp:TreeNode Text="Vincent">
                    <asp:TreeNode Text="Vynn" />
                    <asp:TreeNode Text="Vianne" />
                </asp:TreeNode>
                <asp:TreeNode Text="Maverick" />
            </Nodes>
        </asp:TreeView>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </form>
</body>
</html>





代码背后:





CODE BEHIND:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace WebFormDemo
{
    public partial class TreeView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) {

        }

        protected void Button1_Click(object sender, EventArgs e) {

            bool found = false;
            foreach (TreeNode node in TreeView1.Nodes) {
                found = IsNodeMatch(node, TextBox1.Text);

                if (!found) {
                    foreach (TreeNode childNode in node.ChildNodes) {
                        found = IsNodeMatch(childNode, TextBox1.Text);
                    }
                }
            }

            if (!found)
                Response.Write("Search did not match anything.");

        }

        private bool IsNodeMatch(TreeNode node, string searchText) {
            if (node.Text.ToLower().Equals(searchText.ToLower())) {
                node.Text = string.Format("<div style='color:#ff9900' >{0}</div>", node.Text);
                return true;
            }
            else {
                node.Text = ParseText(node.Text);
            }

            return false;
        }

        private string ParseText(string text) {
            string pattern = "<div(.*?)>(.*?)</div>";
            var regex = new Regex(pattern);

            var match = regex.Match(text);
            if (match.Length > 0)
                return match.Groups[2].Value;

            return text;
        }

    }
}





关键是 ParseText() else 块中用来减少 div element。



The key there is the ParseText() that is used within the else block to trim down the div element.


这篇关于在树视图中更改节点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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