Get checked node text and NavigateUrl in asp.net TreeView

TreeView checked node text and navigate url
TreeView has ability to display a checkbox next to each node. checkbox allow us to select treeview nodes. treeview ShowCheckBoxes property help us to show checkbox on treeview specific nodes. a node can be one mode at a time from selection mode and navigation mode. selection mode is treeview node's default mode. but you can set a node mode to navigation mode. just put a value to NavigateUrl property value other than empty string ("") to convert a node to navigate mode. if you set the NavigateUrl property value empty string then it will make the node mode to selection mode. this c# example code demonstrate us how can we get the treeview checked nodes text and it's navigate url programmatically.
TreeViewCheckedNode.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if(!this.IsPostBack)
        {
            TreeView1.ForeColor = Color.FloralWhite;
            TreeView1.BackColor = Color.SandyBrown;
            TreeView1.BorderWidth = 2;
            TreeView1.BorderColor = Color.SaddleBrown;
            TreeView1.Font.Italic = true;
            TreeView1.Width = 300;
            TreeView1.ParentNodeStyle.Font.Bold = true;
            TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
        }
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.Text = "You checked";
        foreach (TreeNode node in TreeView1.CheckedNodes)
        {
            Label1.Text +="<br />"+ node.Text + " [" + node.NavigateUrl + "]";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get checked node text and NavigateUrl in TreeView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">TreeView Example: Get Checked Nodes</h2>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <asp:Label 
             ID="Label1" 
             runat="server"
             ForeColor="Tomato"
             Font-Italic="true"
             Font-Size="Large"
             Font-Bold="true"
             >
        </asp:Label>
        <br /><br />
        <asp:TreeView 
             ID="TreeView1"
             runat="server"
             DataSourceID="SiteMapDataSource1"
             >
        </asp:TreeView>
        <br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             ForeColor="Salmon"
             Text="Show Checked Node Details"
             Height="45"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>
Web.sitemap
Web.sitemap source code here.

   


Related asp.net example