In this article I will explain with an example, how to get ASP.Net TreeView selected node Text and Value on client side using JavaScript in C# and VB.Net.
 
 
HTML Markup
The HTML Markup consists of:
TreeView – For displaying data.
The TreeView consists of the SelectedNodeStyle property.
Button – For displaying selected Text and Value of selected node.
The Button has been assigned with JavaScript OnClientClick event handler.
<asp:TreeView ID="tvFruits" runat="server">
    <SelectedNodeStyle ForeColor="Black" />
</asp:TreeView>
<br />
<asp:Button ID="btnDisplay" runat="server" Text="Get Selected Node" OnClientClick="return GetSelectedNode();" />
 
 
Getting selected node Text and Value on Client Side using JavaScript
When the Button is clicked, the GetSelectedNode JavaScript function is called.
Inside the GetSelectedNode JavaScript function, the TreeView data is retrieved using dynamically generated identifier to access it.
Then, a checked is performed whether the value of the selected Node is empty or not, if it is not then the Value and Text of the node is fetched and displayed using JavaScript Alert Message Box.
Finally, FALSE is returned.
<script type="text/javascript">
    function GetSelectedNode() {
        var treeViewData = window["<%=tvFruits.ClientID%>" + "_Data"];
        if (treeViewData.selectedNodeID.value != "") {
            var selectedNode = document.getElementById(treeViewData.selectedNodeID.value);
            var value = selectedNode.href.substring(selectedNode.href.indexOf(",") + 3, selectedNode.href.length - 2);
            var text = selectedNode.innerHTML;
            alert("Text: " + text + "\r\nValue: " + value);
        } else {
            alert("No node selected.")
        }
        return false;
    }
</script>
 
 
Populating TreeView
Inside the Page_Load event handler, the name and value of fruits are added as Node to the TreeView.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        tvFruits.Nodes.Add(new TreeNode("Mango", "Fruit 1"));
        tvFruits.Nodes.Add(new TreeNode("Apple", "Fruit 2"));
        tvFruits.Nodes.Add(new TreeNode("Pineapple", "Fruit 3"));
        tvFruits.Nodes.Add(new TreeNode("Orange", "Fruit 4"));
        tvFruits.Nodes.Add(new TreeNode("Grapes", "Fruit 5"));
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        tvFruits.Nodes.Add(New TreeNode("Mango", "Fruit 1"))
        tvFruits.Nodes.Add(New TreeNode("Apple", "Fruit 2"))
        tvFruits.Nodes.Add(New TreeNode("Pineapple", "Fruit 3"))
        tvFruits.Nodes.Add(New TreeNode("Orange", "Fruit 4"))
        tvFruits.Nodes.Add(New TreeNode("Grapes", "Fruit 5"))
    End If
End Sub
 
 
Screenshot
ASP.Net TreeView - Get selected node Text and Value Client Side using JavaScript
 
 
Demo
View Demo
 
 
Downloads