This is a migrated thread and some comments may be shown as answers.

Is it possible to set Node.DataItem when dynamically creating a TreeView?

5 Answers 460 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 2
Adam asked on 13 May 2008, 09:24 PM
I have a RadTreeView that maps a directory structure defined in our database.  We are creating the treeview dynamically (load-on-demand by handling the nodexpand event).  We won't know how many sibling folders will exist at one time, but we have noticed that when there are lots, IE throws "javascript taking too long" warnings. 

We have a workaround that will group large numbers of sibling folders into "virtual folders"  So if we have folders 1...1000, we can set it up to group them into folders [1-100], [101-200], and so on.  In order for this implementation to work completely, I need a way to assign a dataitem to the node while it is dynamically populated, so that I can access this object in the NodeExpand handler to add the virtual folder's children.  However when I assign the DataItem as the node is added, the property is null in the NodeExpand handler.  Since we are already doing dynamic creation and not databinding, is it possible to assign this and have it available later?

FYI: Elsewhere in our code we have this working with the RadComboBox:
    RadComboBoxItem c = new RadComboBoxItem("name", "value");
    RadComboBox1.Items.Add(c);
    RadComboBox1.DataBind();

Thank you.
-Adam

5 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 14 May 2008, 07:02 AM
Hi Adam,

The DataItem attribute is accessible only in the NodeDataBound event allowing you to retrieve data from the datasource.

In other words, you would be able to use the DataItem property if you bind the treeview in the NodeExpand event and then hook on the NodeDataBound to access the e.Node.DataItem (from the event arguments) that is being bound to the node.

Hope this helps.

Regards,
Nick
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Adam
Top achievements
Rank 2
answered on 14 May 2008, 12:08 PM
The problem is, I need to access the DataItem in the node expand, because this object contains the next level of folder information to add to the tree.  Also, do I need to assign a datasource for it to call databind?  I am programmatically creating the tree rather than using a datasource.

I've tried a simple example, and I can't seem to get the NodeDataBound event to fire at all.  Maybe you can point out what I am missing:

C#

 

public

partial class _Default : System.Web.UI.Page

{

private int DirectoryID

{

get { return (int)ViewState["DirID"]; }

set { ViewState.Remove("DirID"); ViewState.Add("DirID", value); }

}

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

Initialize(

1);

}

public void Initialize(int aiDirectoryID)

{

DirectoryID = aiDirectoryID;

PopulateTreeView(

new cStorageDirectory(DirectoryID, "RootNode"));

}

private void PopulateTreeView(cStorageDirectory oDir)

{

RadTreeNode rootNode = new RadTreeNode(oDir.Name, oDir.ID.ToString());

rootNode.DataItem = oDir;

rootNode.Expanded =

true;

rootNode.Category =

"Folder";

rootNode.ImageUrl =

"Images/mailfolder.gif";

rootNode.DataBind();

RadTreeView1.Nodes.Add(rootNode);

RadTreeNode childNode = new RadTreeNode("Child", "5");

RadTreeNode childTwo = new RadTreeNode("SubChild", "6");

rootNode.Nodes.Add(childNode);

childNode.Nodes.Add(childTwo);

RadTreeView1.DataBind();

}

protected void RadTreeView1_NodeDataBound(object sender, RadTreeNodeEventArgs e)

{

Console.WriteLine("Node Data Bound");

}
}




ASPX:

<%

@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%

@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

html xmlns="http://www.w3.org/1999/xhtml" >

<

head runat="server">

<title>Untitled Page</title>

</

head>

<

body>

<form id="form1" runat="server">

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">

</telerik:RadScriptManager>

<div>

<telerik:RadTreeView ID="RadTreeView1" runat="server" OnNodeDataBound="RadTreeView1_NodeDataBound"

OnNodeExpand="RadTreeView1_NodeExpand">

<CollapseAnimation Duration="100" Type="OutQuint" />

<ExpandAnimation Duration="100" />

</telerik:RadTreeView>

</div>

</form>

</

body>

</

html>

0
Nikolay
Telerik team
answered on 15 May 2008, 12:14 PM
Hello Adam,

Actually, you do not need to assign a datasource to call the DataBind method. However, if you do not set a datasource, the DataItem will be empty in the NodeBound event, as there will not be any data items to bind the TreeNodes to. What exactly do you need to store in the DataItem property?

Regards,
Nick
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Adam
Top achievements
Rank 2
answered on 15 May 2008, 12:29 PM
I am needing to store the database key id values for the children nodes underneath the node that was selected.

The database contains a directory structure.  Every time a node is expanded, we are adding the children nodes to the node that was expanded, following your Load-On-Demand examples.  However, we may have 10,000 or 100,000 sibling nodes in any given directory.  So we are recognizing large numbers of sibling nodes and instead of adding the nodes directly, we are making virtual folders.  So if we have folders 1-1000, we may create 10 virtual folders, each containing a subset of the 1000 folders.  So the first virtual folder would contain folders 1 through 100, the second 101-200, and so forth.  The virtual folders are created programmatically, but they are not stored in the database and beyond creation we have no way of recreating them (we've lost reference to the parent node when it comes time to expand the virtual folder)

Since first posting my question, I have played around quite a bit with a few different methods of this, and I came up with a solution using the custom Attributes object.  I store a flag on whether the node is a virtual folder, and if it is, I also store a delimited list of all of the database keys for the children nodes that this folder contains.  It seems to work really well.  This is ultimately what I was trying to acheive through the use of the DataItem.  Assigning an object containing sub-objects to DataItem when the virtual node is first created would be a little bit more classy of a solution, but the delimted attribute string seems to work.
0
Simon
Telerik team
answered on 15 May 2008, 01:08 PM
Hi Adam,

Another thing you could try is to use Custom Attributes to store the needed data on the first Data Bind of the RadTreeView. Later in the NodeExpand event you could access this data and use it to perform the needed operations.

All the best,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
TreeView
Asked by
Adam
Top achievements
Rank 2
Answers by
Nikolay
Telerik team
Adam
Top achievements
Rank 2
Simon
Telerik team
Share this question
or