asp.net - Get checked child nodes parent node text from a TreeView

TreeView checked child nodes parent node text
TreeView control is made up of nodes. treeview control's every entry called node. node is represented by a TreeNode object. there are four types of node. root node, parent node, child node and leaf node. root node is a node that is not contained by any other node. parent node contains other node. child node is contained by other node. and leaf node has no child node. so the each child node have a parent node. the following example source code demonstrate you how can we get checked node's parent node text in treeview.
TreeViewGetCheckedParent.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.OliveDrab;
            TreeView1.BorderColor = Color.DarkSeaGreen;
            TreeView1.Width = 300;
            TreeView1.BorderWidth = 3;
            TreeView1.ParentNodeStyle.Font.Bold = true;
            TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
            TreeView1.LeafNodeStyle.Font.Italic = true;
        }
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.Text = "You checked";
        foreach (TreeNode node in TreeView1.CheckedNodes)
        {
            Label1.Text += "<br />" + node.Text;
            Label1.Text += "<i><b>..." + node.Parent.Text + "</b></i>";
        }
    }
</script>

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

   


Related asp.net example