Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Basically I am trying to create a Treeview function that highlights a node based off a value I give it.

The page loads data from a session and if that session isnt empty it loads the NodeID from the session and highlights it. If the session is empty its the first use so it waits for an input to set it using the function below

The onClick function that first sets the NodeID here, which is put into a session by another function:

C#
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{    

TreeView1.SelectedNodeStyle.BackColor = System.Drawing.Color.LightGreen;
dbSession.NodeID = Convert.ToInt32(TreeView1.SelectedNode.Value.ToString());
           
}

and I have to keep my treeview control close to this, as the treeview is generated by looking into a database table and gets the order out ( I already have this set up)


XML
<asp:TreeView ID="TreeView1" runat="server" onselectednodechanged="TreeView1_SelectedNodeChanged" Font-Size="Large" Height="272px" Width="269px">





The function I need advise on would take the NodeID and select the Treeview Control node from that number and highlight it.

Any advise on how this is done? I've been unsuccessful in finding any information on "Selecting a node from an ID Variable" instead of on click then highlighting that node
Posted
Updated 5-Sep-11 23:00pm
v2

1 solution

Hi you can store the value instead of searching for id of the selected node in session and select/highlight the node based on the value stored in session.

Please see below code: tree view HTML
HTML
<div>
       <asp:treeview id="Treeview1" runat="server" xmlns:asp="#unknown">
           <nodes>
               <asp:treenode text="Type">
                   <asp:treenode value="IN" text="Inpatient"></asp:treenode>
                   <asp:treenode text="Outpatient"></asp:treenode>
               </asp:treenode>
           </nodes>
       </asp:treeview>
   </div>

See below code behind. I have stored one value in session and in page load i am iterating through the tree and finding out the node having the same value and then making that one selected.
C#
protected void Page_Load(object sender, EventArgs e)
    {
        Session["selValue"] = "IN";
        TreeNodeCollection col =  Treeview1.Nodes;
        highlightNode(col);
    }

    private void highlightNode(TreeNodeCollection col)
    {
        foreach (TreeNode node in col)
        {
            if (node.ChildNodes.Count > 0)
            {
                highlightNode(node.ChildNodes);
            }
            if (node.Value.Trim().ToUpper() == Session["selValue"].ToString().Trim().ToUpper())
            {
                node.Selected = true;
                node.Expanded = true;
                break;
            }
        }
    }

See the below javascript code in order to highlight the selected node.

JavaScript
var HighlightSelectedRow = function ( ){
  var selId = $("[ID$=_SelectedNode]").val(); 
  $("a#"+selId).css("backgroundColor","Yellow");
}
$(function ( ){
  HighlightSelectedRow();
})


You can put this javascript code at the end of your html page.
Change the color from Yellow to something that you want.

Hope it helped.
 
Share this answer
 
Comments
RaviRanjanKr 3-Sep-11 22:54pm    
Nice Answer, My 5+
Phoenix234 6-Sep-11 5:09am    
I've realised this wont for for the system I'm working on, Basically the treeview is created using code that processes a database table i created, my issue is that until you actually click the treeview it wont highlight where as i want it highlighting by number not click

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