Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to change the forecolor of the selectednode. I am using following code::

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
 {

TreeView1.SelectedNodeStyle.ForeColor = Color.Maroon;

}


But i have a issue suppose i have a tree of State with nodes CA,WA,AK,AZ. When i select CA its becomes Marron and when i select WA its becomes Marron but CA becomes normal. I want all(which are selected) indivually are Marron.


Please help me

Thanks

Pankaj

[EDIT]Formatted code. Please remember to do this before posting again[/EDIT]
Posted
Updated 3-Jun-10 6:15am
v2

When you click the node it of course causes a postback which resets the nodes. If you only need to change the color I would suggest handling it with JavaScript, otherwise you will need to reset the color of the selected nodes during the postback
 
Share this answer
 
The problem is not because of PostBack not maintaining the viewstate... ASP.Net should do that just fine, assuming you don't databind again.

The problem is that you are setting "SelectedNodeStyle" rather than the style of the currently selected node. In fact, you cannot set the style for particular nodes (the ASP.Net TreeView only has styles for particular groups of nodes, such as the selected node and the parent node).

You will have to render a custom TreeNode. This page has a pretty good example of how to do some custom tree node rendering. Also, I'm not too familiar with maintaining view state, but you may want to look into how to do that so you don't have to set each previously selected node's style on each postback.
 
Share this answer
 
Comments
[no name] 3-Jun-10 14:51pm    
Doesn't properly address using JavaScript to handle it.
AspDotNetDev 3-Jun-10 18:20pm    
JavaScript is not necessary and would be an advanced topic if the OP doesn't know JavaScript. Even if JavaScript were used, there would still need to be some handling of the view state to maintain the color information on postbacks (postbacks unrelated to tree view selection changes).

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