Insert a Node Dynamically
To dynamically insert a node, complete the following:
1. Add a PlaceHolder control to the Web page.
2. Add a LinkButton server control using declarative syntax:
<br/>     <asp:LinkButton ID="lnkInsertNode" Text="Insert new Node" OnClick="ClickInsertNode" runat="server"></asp:LinkButton>
The OnClick attribute is included in the markup so when you click on the LinkButton control, the event will call the ClickInsertNode event handler which we will later create.
3. Add the directive for the namespace to your source code.
Imports C1.Web.UI.Controls.C1TreeView
• C#
using C1.Web.UI.Controls.C1TreeView
4. Use the following code to create the dynamic treeview:
Private treeView As C1.Web.UI.Controls.C1TreeView.C1TreeView
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'create an instance of the class
treeView = New C1.Web.UI.Controls.C1TreeView.C1TreeView()
treeView.ID = "TreeView"
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 = "Products 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
• C#
private C1.Web.UI.Controls.C1TreeView.C1TreeView treeView;
protected void Page_Load(object sender, EventArgs e)
{
//create an instance of the class
treeView = new C1.Web.UI.Controls.C1TreeView.C1TreeView();
treeView.ID = "TreeView";
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 = "Products 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);
}
}
5. Create a ClickInsertNode event handler to raise an event when you click the Insert Node link button. This event will call the CreateNode method to add a new C1TreeViewNode.
Protected Sub ClickInsertNode(ByVal sender As Object, ByVal e As EventArgs)
treeView.Nodes.Insert((treeView.Nodes.Count - 1), CreateNode())
End Sub
• C#
protected void ClickInsertNode(object sender, EventArgs e)
{
treeView.Nodes.Insert((treeView.Nodes.Count - 1), CreateNode());
}
6. Use the CreateNode() function to dynamically add a new C1TreeViewNode when the LinkButton is clicked.
Private Function CreateNode() As C1TreeViewNode
Dim node As New C1TreeViewNode()
node.Text = "New Node"
node.Value = "NewNode"
Return node
End Function
• C#
private C1TreeViewNode CreateNode()
{
C1TreeViewNode node = new C1TreeViewNode();
node.Text = "New Node";
node.Value = "NewNode";
return node;
}
7. Run your project and click on the Insert new Node link button to insert a new node.
|