Tuesday, May 5, 2009

Retrieve selected treenode value at server side

As i said in my previous post here’s the way to retrieve selected treenode value at server side.

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

Here’s the output of selected node:

data

C#

  1:  protected void btnGetNode_Click(object sender, EventArgs e)
  2:  {
  3:         TreeNodeCollection tn = tvSample.CheckedNodes;
  4:         for (int i = 0; i < tn.Count; i++)
  5:         {
  6:             String[] arr =  tn[i].Value.Split('$');
  7:             if (arr.Length == 2)
  8:             {
  9:                 Response.Write("<br>Parent Node Value" + arr[1] + "<br>");
 10:                 Response.Write("<br>Child Node: " + tn[i].Text + " Value: " + arr[0]);
 11:             }
 12:         }
 13:  }

VB
  1: Protected Sub btnGetNode_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetNode.Click
  2:         Dim tn As TreeNodeCollection = tvSample.CheckedNodes
  3:         For i As Integer = 0 To tn.Count - 1
  4:             Dim arr As [String]() = tn(i).Value.Split("$"c)
  5:             If arr.Length = 2 Then
  6:                 Response.Write("<br>Parent Node Value" & arr(1) & "<br>")
  7:                 Response.Write("<br>Child Node: " & tn(i).Text & " Value: " + arr(0))
  8:             End If
  9:         Next
 10:     End Sub
 11: 
As shown in above code, we are splitting node value with ‘$’ sign. The first value is parent node value and second value is child node value. Another way is to loop through all the nodes and check for checked node value.

I hope this helps!! :)

In my next post i will write about check tree nodes on the basis of database value.

No comments: