ComponentOne TreeView for ASP.NET WebForms
TreeView Creation / Dynamic Creation
In This Topic
    Dynamic Creation
    In This Topic

    Dynamic treeviews can be created on the server side or client side. When creating dynamic treeview on the server side, use a constructor to dynamically create a new instance of the C1TreeViewNode class. For client-side, the CreateInstance constructor can be used to dynamically create a new instance of the C1TreeView control. For example the follow script creates a new C1TreeView control on the client side:


    var aTreeView = C1.Web.C1TreeView.createInstance ();
    document.body.appendChild(aTreeView.element);
    

    C1TreeView or C1TreeViewNode contructors can be used to create a new instance of the C1TreeView or C1TreeViewNode class. Once the nodes are created, they can be added to the Node collection of a new node or treeview.

    For example:

    To write the code in Visual Basic:

    Visual Basic
    Copy Code

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        'create an instance of the class
        Dim treeView As New C1TreeView()
        PlaceHolder1.Controls.Add(treeView)
       

        If Not Page.IsPostBack Then
           Dim P As New C1TreeViewNode()
                 P.Text = "Products"
                 P.Value = "PS"
                 P.Expanded = True
                 treeView.Nodes.Add(P)
           

           Dim Pr1 As New C1TreeViewNode()
                Pr1.Text = "Product 1"
                Pr1.Value = "Pr1"
                Pr1.Expanded = True
                P.Nodes.Add(Pr1)
           

            Dim Oview1 As New C1TreeViewNode()
               Oview1.Text = "Overview"
               Oview1.Value = "Oview1"
               Pr1.Nodes.Add(Oview1)
           

           Dim Down1 As New C1TreeViewNode()
               Down1.Text = "Downloads"
               Down1.Value = "Down1"
               Pr1.Nodes.Add(Down1)
           

           Dim Supp1 As New C1TreeViewNode()
               Supp1.Text = "Support"
               Supp1.Value = "Supp1"
               Pr1.Nodes.Add(Supp1)
           

           Dim Pr2 As New C1TreeViewNode()
               Pr2.Text = "Products 2"
               Pr2.Value = "Pr2"
               Pr2.Expanded = True
               P.Nodes.Add(Pr2)
           

           Dim Oview2 As New C1TreeViewNode()
               Oview2.Text = "Overview"
               Oview2.Value = "Oview2"
               Pr2.Nodes.Add(Oview2)
           

           Dim Down2 As New C1TreeViewNode()
               Down2.Text = "Downloads"
               Down2.Value = "Down2"
               Pr2.Nodes.Add(Down2)
           

           Dim Supp2 As New C1TreeViewNode()
              Supp2.Text = "Support"
              Supp2.Value = "Supp2"
              Pr2.Nodes.Add(Supp2)
       

       End If
    End Sub

    To write the code in C#:

    C#
    Copy Code

     protected void Page_Load(object sender, EventArgs e)
         {
             //create an instance of the class
             C1TreeView treeView = new C1TreeView();
             PlaceHolder1.Controls.Add(treeView);
           

           if (!Page.IsPostBack)
               {
                   C1TreeViewNode P = new C1TreeViewNode();
                      P.Text = "Products";
                      P.Value = "PS";
                      P.Expanded = true;
                      treeView.Nodes.Add(P);
                 

                  C1TreeViewNode Pr1 = new C1TreeViewNode();
                      Pr1.Text = "Product 1";
                      Pr1.Value = "Pr1";
                      Pr1.Expanded = true;
                      P.Nodes.Add(Pr1);
                     

                  C1TreeViewNode Oview1 = new C1TreeViewNode();
                      Oview1.Text = "Overview";
                      Oview1.Value = "Oview1";
                      Pr1.Nodes.Add(Oview1);
                 

                 C1TreeViewNode Down1 = new C1TreeViewNode();
                      Down1.Text = "Downloads";
                      Down1.Value = "Down1";
                      Pr1.Nodes.Add(Down1);
                

                 C1TreeViewNode Supp1 = new C1TreeViewNode();
                     Supp1.Text = "Support";
                     Supp1.Value = "Supp1";
                     Pr1.Nodes.Add(Supp1);
                 

                C1TreeViewNode Pr2 = new C1TreeViewNode();
                     Pr2.Text = "Products 2";
                     Pr2.Value = "Pr2";
                     Pr2.Expanded = true;
                     P.Nodes.Add(Pr2);
                

                C1TreeViewNode Oview2 = new C1TreeViewNode();
                     Oview2.Text = "Overview";
                     Oview2.Value = "Oview2";
                     Pr2.Nodes.Add(Oview2);
               

                C1TreeViewNode Down2 = new C1TreeViewNode();
                     Down2.Text = "Downloads";
                     Down2.Value = "Down2";
                     Pr2.Nodes.Add(Down2);
               

               C1TreeViewNode Supp2 = new C1TreeViewNode();
                    Supp2.Text = "Support";
                    Supp2.Value = "Supp2";
                    Pr2.Nodes.Add(Supp2);
             }
         }

    See Also