Remove a Node Dynamically
To dynamically remove 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="lnkRemoveNode" Text="Remove Node" OnClick="ClickRemoveNode" 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 ClickRemoveNode event handler which we will create in the following step.
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 ClickRemoveNode event handler to raise an event when you click the Remove Node link button. This event will call the RemoveAt method to remove a C1TreeViewNode.
Protected Sub ClickRemoveNode(sender As Object, e As EventArgs)
If (treeView.Nodes.Count - 1) >= 0 Then
treeView.Nodes.RemoveAt(treeView.Nodes.Count - 1)
End If
End Sub
• C#
protected void ClickRemoveNode(object sender, EventArgs e)
{
if ((treeView.Nodes.Count - 1) >= 0)
treeView.Nodes.RemoveAt(treeView.Nodes.Count - 1);
}
}
|