Retrieve selected treenode value at server side

As I said in my previous post, here’s how to retrieve selected tree node values on the server side.

Treeview with checkboxes

All the nodes, including the parent node, are selected in the above image. In my first post, we concatenated the parent node’s value with the child node and assigned it to the child node so that whenever we want to retrieve the value of the child node, we can have the parent node value.

Here’s the output of the selected nodes:



protected void btnGetNode_Click(object sender, EventArgs e)
     TreeNodeCollection tn = tvSample.CheckedNodes;
     for (int i = 0; i < tn.Count; i++)
          String[] arr =  tn[i].Value.Split('$');
          if (arr.Length == 2)
               Response.Write("<br>Parent Node Value" + arr[1] + "<br>");
               Response.Write("<br>Child Node: " + tn[i].Text + " Value: " + arr[0]);


Protected Sub btnGetNode_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetNode.Click
     Dim tn As TreeNodeCollection = tvSample.CheckedNodes
     For i As Integer = 0 To tn.Count - 1
          Dim arr As [String]() = tn(i).Value.Split("$"c)
          If arr.Length = 2 Then
               Response.Write("<br>Parent Node Value" & arr(1) & "<br>")
               Response.Write("<br>Child Node: " & tn(i).Text & " Value: " + arr(0))
          End If
End Sub

The above code shows that we split the node value with the ‘$’ sign. The first value is the parent node value, and the second is the child node value. Another way is to loop through all the nodes and check for the checked node value.

I hope this helps!

In my next post, I will write about checking tree nodes based on database value.





Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: