Home - Forums-.NET - FlyTreeView (ASP.NET) - getting node info after clicking on a button on client

FlyTreeView (ASP.NET)

Technical support and KB related to the FlyTreeView control

This forum related to following products: FlyTreeView for ASP.NET

getting node info after clicking on a button on client
Link Posted: 06-Oct-2008 12:00
Here's a dumb question but I can't find any examples on this documentation page that will help me:
http://www.9rays.net/asp.net_2/treeview/miscpages/doc/ClientObjects.aspx

I have an asp.net web page w/ two flytreeview controls.
I also have a regular  button.  When this is clicked, I want to be able to get a path to the node that is selected in that treeview and also the value at that node.  This has to be done on  both trees.
How do I do this for multiple flytreeviews?  I found a few examples that seem to assume you only have a single flytreeview on your web page...

thanks,

ken
Link Posted: 06-Oct-2008 14:23
Ken,

You can use the following code to get value of selected node:
CFlyTreeView.getInstanceById('').getSelectedNode().getValue()
Link Posted: 07-Oct-2008 04:15
Thanks Evgeny, but it doesn't seem to work.  I set up the treeview to use radio buttons for the entire tree so that might be different from what you tried (you probably used a regular treeview w/ checkboxes).

Here's my code:
        var tree = CFlyTreeView.getInstanceById('');
        var selnode = tree.getSelectedNode();
        if (selnode == undefined) {
          alert('no node selected');
        } else {
          alert('selected is' + selnode.getValue());
        }        

selnode is always undefined.
Link Posted: 07-Oct-2008 08:23
Ken,

I took your code and created a small test page:


    
        
        
    
    

Click Me!


function showSelectedValue()
{
    var tree = CFlyTreeView.getInstanceById('');
    var selnode = tree.getSelectedNode();
    if (selnode == undefined) {
        alert('no node selected');
    } else {
        alert('selected is' + selnode.getValue());
    }
}


How do you call your code? Do you call it during page load? If yes, then you should wait until FlyTreeView initializes (using FlyTreeView.OnInitializedJavascript handler).
Link Posted: 07-Oct-2008 08:41
That should in theory be the same (I was asssuming the radio button was causing it)

The real treeview is a bit more complicated...it loads data dynamically instead of having hardcoded nodes.
I'll try to strip it down so we can find out what's going on.

The code is not done on page load.  It's done via a button just like you have.  I'm trying to make the button copy some info on the selected nodes from the two trees into a listbox.
Link Posted: 07-Oct-2008 08:44
Test page that reproduces issue would be perfect.
Link Posted: 07-Oct-2008 09:22
Yep, it had to do with dynamic generation of the tree. Click on the Add button. It doesn't do anything currently except for trying to display the node value. Try this code: [code] <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestTreeView.aspx.cs" Inherits="TestTreeView" Title="XY Chart" %> <%@ Register Assembly="NineRays.WebControls.FlyTreeView" Namespace="NineRays.WebControls" TagPrefix="NineRays" %> [/code] [code] using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Globalization; using System.Collections.Generic; public partial class TestTreeView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { initTree(topTree, "top"); initTree(bottomTree, "bottom"); } protected void topTree_PopulateNodes(object sender, NineRays.WebControls.FlyTreeNodeEventArgs e) { tree_PopulateNodes(e.Node); } protected void bottomTree_PopulateNodes(object sender, NineRays.WebControls.FlyTreeNodeEventArgs e) { tree_PopulateNodes(e.Node); } protected void tree_PopulateNodes(NineRays.WebControls.FlyTreeNode node) { NineRays.WebControls.FlyTreeNode tracknode = new NineRays.WebControls.FlyTreeNode("child1", "child" + System.Guid.NewGuid().ToString()); tracknode.IsCheckbox = true; tracknode.RadioGroup = node.TreeView.ID; node.ChildNodes.Add(tracknode); NineRays.WebControls.FlyTreeNode tracknode2 = new NineRays.WebControls.FlyTreeNode("child2", "child" + System.Guid.NewGuid().ToString()); tracknode2.IsCheckbox = true; tracknode2.RadioGroup = node.TreeView.ID; node.ChildNodes.Add(tracknode2); } private void initTree(NineRays.WebControls.FlyTreeView tree, string name) { NineRays.WebControls.FlyTreeNode devnode = new NineRays.WebControls.FlyTreeNode(name, tree.ID); devnode.PopulateNodesOnDemand = true; tree.Nodes.Add(devnode); } } [/code]
Link Posted: 07-Oct-2008 23:29
Oh, I see you're using getSelectedNode() function, but rather you need to find a checked node. This is not the same.

Your code should look like:
var tree = CFlyTreeView.getInstanceById('');
var checkedNode = tree.find(function(node) { return node.getChecked(); });

if (checkedNode == undefined) {
  alert('no node checked');
} else {
  alert('checked is' + checkedNode.getValue());
}


Also, for your code-behind, I moved initTree calls into !IsPostBack condition:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        initTree(topTree, "top");
        initTree(bottomTree, "bottom");
    }
}

Just to prevent adding additional root node every postback/callback.
Link Posted: 08-Oct-2008 03:02
I get a javascript error that the tree.find() function doesn't exist.

In the debugger, I can see findAll, findByPath, findByValue, and findByID.

Also, how would this work if you have two or more groups or radio buttons in the same tree?

Quick suggestion: Using that tree.find() function is non-intuitive.  The tree.getSelectedNodes() function should just return an array of selected nodes no matter whether the tree is in checkbox mode or radiobutton mode...
Link Posted: 08-Oct-2008 03:10
Got it to work doing this:

        var checkedNodes = tree.findAll(function(node) { return node.getChecked(); });
        if (checkedNodes.length == 0) {

You can then get the first checked node by doing this:
  var checkedNode = checkedNodes[0]

thanks, Evgeny



" onclick=" var tree = CFlyTreeView.getInstanceById(''); var selnode = tree.getSelectedNode(); if (selnode == undefined) { alert('no node selected'); } else { alert('selected is' + selnode.getValue()); } " />