asp.net - How to add a node in a TreeView programmatically

Add TreeNode in TreeView control
TreeViewAddNode.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        TreeNode treeNode = new TreeNode();
        treeNode.Text = TextBox1.Text;
        treeNode.NavigateUrl = TextBox2.Text;
        TreeView1.FindNode("Home/DataToolBox").ChildNodes.Add(treeNode);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to add tree node (TreeNode) in TreeView control programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">TreeView Example: Add TreeNode</h2>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <asp:TreeView 
             ID="TreeView1"
             runat="server"
             DataSourceID="SiteMapDataSource1"
             Width="250"
             BorderWidth="2"
             BorderColor="Crimson"
             >
             <LeafNodeStyle BackColor="Crimson" ForeColor="LightGoldenrodYellow" />
        </asp:TreeView>
        <br />
        <asp:Label 
             ID="Label1"
             runat="server"
             Font-Bold="true"
             ForeColor="SeaGreen"
             Text="Node Text"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="SeaGreen"
             ForeColor="LightGoldenrodYellow"
             >
        </asp:TextBox>
        <br />
        <asp:Label 
             ID="Label2"
             runat="server"
             Font-Bold="true"
             ForeColor="SeaGreen"
             Text="Navigate Url"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox2"
             runat="server"
             BackColor="SeaGreen"
             ForeColor="LightGoldenrodYellow"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             ForeColor="SeaGreen"
             Text="Add Node To TreeView"
             Height="45"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>
Web.sitemap
Web.sitemap source code here.

   


More asp.net examples