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
<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.
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.
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.