soundy
soundy

Reputation: 307

How to change color in asp.net(c#) . if treenode checked

In my ASP.NET application, used Treeview control with check box enabled. In that treeview if i checked treenode want to show different color in specific checked node. see below

 foreach (treenode node in treeview.nodes)
 {
    if (node.checked == true)
    {
      "change the color of the node" 
    }   
 }

i used to check the nodes like this below coding . but no tag to color change of checked nodes

    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {           

        if (e.Node.ChildNodes.Count > 0)
        {
            CheckAllChildNodes(e.Node, e.Node.Checked);
        }

        if (e.Node.ChildNodes.Count == 0)
        {
            CheckAllParentNodes(e.Node);
        }
     }

     private void CheckAllChildNodes(System.Web.UI.WebControls.TreeNode treeNode, bool nodeChecked)
    {
        foreach (System.Web.UI.WebControls.TreeNode node in treeNode.ChildNodes)
        {
            node.Checked = nodeChecked;

            if (node.ChildNodes.Count > 0)
            {
                this.CheckAllChildNodes(node, nodeChecked);
            }
        }
    }
    private void CheckAllParentNodes(System.Web.UI.WebControls.TreeNode treeNode)
    {
        if (treeNode.Parent != null)
        {
            if (treeNode.Checked == false)
            {
                treeNode.Parent.Checked = false;
                CheckAllParentNodes(treeNode.Parent);
            }
        }
    }

Please help me to solve this thing ..

Upvotes: 1

Views: 11135

Answers (2)

Adil
Adil

Reputation: 148178

Setting the BackColor property of SelectedNode could make it automatic.

treeview.SelectedNodeStyle.BackColor = System.Drawing.Color.Silver;

EDIT

There is no ForeColor property for individual node in asp.net TreeView. To change the fore color you have to extend TreeNode and apply style in your customized TreeNode Class. You will have to use this customized TreeNode in TreeView Nodes Collection instead of using default TreeNode class. For detailed understanding These articles article 1, article 2 will help you.

In HTML

<asp:TreeView ID="TreeView1" runat="server">
    <Nodes>
        <My:CustomTreeNode Text="Node A"
                           Value="Node A">
            <My:CustomTreeNode Text="Node B"
                               Value="Node B">
            </My:CustomTreeNode>
        </My:CustomTreeNode>
    </Nodes>
</asp:TreeView>

In Code Behind

public class CustomTreeNode : TreeNode
{
    protected override void RenderPreText(HtmlTextWriter writer)
    {
       writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "green");
    }
}

Upvotes: 6

MSUH
MSUH

Reputation: 872

If i understand your question clearly that is a bit tricky, you have to do following steps

1) Set the text of each node of your tree view like this

<asp:TreeNode Text='<font color="Red"> Test Inner 1</font>' Value="1"></asp:TreeNode>

2) Use following code in code behind

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    if(Session["lastNode"] != null)
    {
       TreeNode lastNode = Session["lastNode"] as TreeNode;

       TreeNode tn = TreeView1.FindNode(Server.HtmlEncode(lastNode.ValuePath));
       tn.Text = tn.Text.Replace(@"color=""Red""", @"color=""Blue""");

     }

    Session["lastNode"] = TreeView1.SelectedNode;
}

Upvotes: 2

Related Questions