Hi,
I have a radtreeview control. I bind this during the page load event. I’d like to add some attributes to the nodes.
So far I have done this via the NodeDataBound event. However what I really need to do is store
some data from the original datatable in some attributes.
I guess to do this I need to create the radtreeview by
looping through the datable and creating the nodes individually. When I do this I can’t see a way to set the
parent IDs?
Any suggestions?
5 Answers, 1 is accepted
You are free to continue using the NodeDataBound event. In the database in addition to the default columns id, text, parentId you could add other columns to store some information. As the NodeDataBound event is fired after for each node in the event you have access to the node and to its properties. For example if you have a color column for your node object you could add it as an attribute like in code below:
protected
void
RadTreeView1_NodeDataBound(
object
sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
{
var nodeColor = e.Node.Color;
e.Node.Attributes.Add(
"color"
, nodeColor);
}
Regards,
Anton
Telerik by Progress
Hi Anton,
Sorry I don't follow.
In the page load event I get a datatable. I then link columns from
this to key node fields. So say my datatable has a column called xyz.
How can I ensure that each node has an attribute that relates to the corresponding
value in the datatable?
You example would result
in treeview containing ‘x’ nodes, all of which have an attribute called 'color'
which would have the same value?
If you have a column "xyz" each node would have an xyz value along with id, text and parentId. As i mentioned in the previous post the NodeDataBound event is fired for each node in the TreeView.
So the example from the previous post would result in TreeView containing ‘x’ nodes, all of which have an attribute called 'color' BUT with different value. The value is different because in the NodeDataBound event every time the node is different it has its own color from the database.
Regards,
Anton
Telerik by Progress
hi,
Sorry, I think we're still talking at cross purposes, here is some sample code:
01.
protected
void
Page_Load(
object
sender, EventArgs e)
02.
{
03.
DataTable dt =
new
DataTable();
04.
dt.Columns.Add(
"name"
);
05.
dt.Columns.Add(
"value"
);
06.
dt.Columns.Add(
"parent_id"
);
07.
dt.Columns.Add(
"id"
);
08.
dt.Columns.Add(
"attr_col"
);
09.
10.
dt.Rows.Add(
"name1"
,
"value1"
,
null
, 1, Guid.NewGuid().ToString());
11.
dt.Rows.Add(
"name2"
,
"value2"
, 1, 2, Guid.NewGuid().ToString());
12.
dt.Rows.Add(
"name3"
,
"value3"
, 1, 3, Guid.NewGuid().ToString());
13.
14.
15.
RadTreeView1.DataTextField =
"name"
;
16.
RadTreeView1.DataFieldID =
"id"
;
17.
RadTreeView1.DataValueField =
"value"
;
18.
RadTreeView1.DataFieldParentID =
"parent_id"
;
19.
RadTreeView1.DataSource = dt;
20.
RadTreeView1.DataBind();
21.
}
22.
23.
protected
void
RadTreeView1_NodeDataBound(
object
sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
24.
{
25.
// I want to assign the value of data table column 'attr_col' to this attribute
26.
e.Node.Attributes.Add(
"attr_col"
, ????);
27.
}
I apologize because it seems I misunderstood your scenario, regarding the code you've posted. I modified your example to show how to add custom attributes, using the NodeDataBound event:
protected
void
Page_Load(
object
sender, EventArgs e)
{
DataTable dt =
new
DataTable();
dt.Columns.Add(
"name"
);
dt.Columns.Add(
"value"
);
dt.Columns.Add(
"parent_id"
);
dt.Columns.Add(
"id"
);
dt.Columns.Add(
"attr_col"
);
dt.Rows.Add(
"name1"
,
"value1"
,
null
, 1, Guid.NewGuid().ToString());
dt.Rows.Add(
"name2"
,
"value2"
, 1, 2, Guid.NewGuid().ToString());
dt.Rows.Add(
"name3"
,
"value3"
, 1, 3, Guid.NewGuid().ToString());
RadTreeView1.DataTextField =
"name"
;
RadTreeView1.DataFieldID =
"id"
;
RadTreeView1.DataValueField =
"value"
;
RadTreeView1.DataFieldParentID =
"parent_id"
;
RadTreeView1.DataSource = dt;
RadTreeView1.DataBind();
}
protected
void
RadTreeView1_NodeDataBound(
object
sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
{
DataRowView dataSourceRow = (DataRowView)e.Node.DataItem;
string
attribute = dataSourceRow[
"attr_col"
].ToString();
e.Node.Attributes.Add(
"attributeName"
, attribute);
}
protected
void
RadTreeView1_NodeClick(
object
sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
{
var attribute = e.Node.Attributes[
"attributeName"
];
}
Regards, Anton
Telerik by Progress