I am trying to use ItemTemplate when Databoud, but can't get it to work.
The following code gives an error:
DataBinding: 'Telerik.Web.UI.OrgChartGroupItemRenderer' does not contain a property with the name 'name'.
<
telerik:RadOrgChart
ID
=
"rocUsers"
runat
=
"server"
DataFieldID
=
"ID"
DataFieldParentID
=
"manager"
DataTextField
=
"fullname"
DisableDefaultImage
=
"True"
GroupColumnCount
=
"2"
>
<
ItemTemplate
>
<
strong
><%# DataBinder.Eval(Container, "name") %></
strong
>
</
ItemTemplate
>
<
RenderedFields
>
<
ItemFields
>
<
telerik:OrgChartRenderedField
DataField
=
"name"
Label
=
":"
/>
<
telerik:OrgChartRenderedField
DataField
=
"avdeling_name"
Label
=
":"
/>
</
ItemFields
>
</
RenderedFields
>
</
telerik:RadOrgChart
>
The following code gives no data, only the label Name: is shown in the boxes.
It is no difference if the RenderedFields are set or not.
It works fine if I use RenderedFields and no ItemTemplate, so the data is avaliable.
<
telerik:RadOrgChart
ID
=
"rocUsers"
runat
=
"server"
DataFieldID
=
"ID"
DataFieldParentID
=
"manager"
DataTextField
=
"fullname"
DisableDefaultImage
=
"True"
GroupColumnCount
=
"2"
>
<
ItemTemplate
>
<
asp:Image
ID
=
"Image1"
runat
=
"server"
ImageUrl='<%# Bind("name","~/vimages/ownimages/{0}.jpg") %>'
Width="30px" Height="30px" />
Name:
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text='<%# Eval("Fullname") %>'></
asp:Label
>
<
br
/>
<
asp:Label
ID
=
"Label2"
runat
=
"server"
Text='<%# Bind("avdeling_name") %>'></
asp:Label
>
<
/
ItemTemplate
>
<
RenderedFields
>
<
ItemFields
>
<
telerik:OrgChartRenderedField
DataField
=
"name"
Label
=
":"
/>
<
telerik:OrgChartRenderedField
DataField
=
"avdeling_name"
Label
=
":"
/>
</
ItemFields
>
</
RenderedFields
>
</
telerik:RadOrgChart
>
8 Answers, 1 is accepted
In order to get a field which is present in the dataItem from which the GroupItem is created, your binding expressions should access the DataItem:
<%# DataBinder.Eval(Container.DataItem, "name") %>
In order to access the RenderedFields, you can access directly ((OrgChartGroupItemRenderer)Container).RenderedFields[0].Text.
Nikolay Tsenkov
the Telerik team
<
telerik:RadOrgChart
ID
=
"RadOrgChart1"
runat
=
"server"
>
<
ItemTemplate
>
<
asp:Image
ID
=
"Image1"
runat
=
"server"
ImageUrl
=
"http://2.bp.blogspot.com/_LBAtNnpRN7k/S_DfBlu9Q7I/AAAAAAAAPTE/znoECHLIizM/s200/User+Accounts+in+Windows.jpg"
Width
=
"30px"
Height
=
"30px"
/>
<
br
/>
Name:
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text='<%# Eval("Certificates") %>'></
asp:Label
>
<
br
/>
<
asp:Label
ID
=
"Label2"
runat
=
"server"
Text='<%# Eval("Text") %>'></
asp:Label
>
</
ItemTemplate
>
<
RenderedFields
>
<
NodeFields
>
<
telerik:OrgChartRenderedField
DataField
=
"TeamName"
Label
=
"Team"
/>
</
NodeFields
>
<%-- <
ItemFields
>
<
telerik:OrgChartRenderedField
DataField
=
"Certificates"
Label
=
"Certificates"
/>
<
telerik:OrgChartRenderedField
DataField
=
"Text"
Label
=
"Text"
/>
</
ItemFields
>--%>
</
RenderedFields
>
</
telerik:RadOrgChart
>
protected
void
Page_Load(
object
sender, EventArgs e)
{
DataTable nodeTable =
new
DataTable();
nodeTable.Columns.Add(
"ID"
);
nodeTable.Columns.Add(
"ParentID"
);
nodeTable.Columns.Add(
"TeamName"
);
nodeTable.Rows.Add(
new
String[] {
"1"
,
null
,
"Management"
});
nodeTable.Rows.Add(
new
String[] {
"2"
,
"1"
,
"Unit"
});
nodeTable.Rows.Add(
new
String[] {
"3"
,
"2"
,
"Dev"
});
nodeTable.Rows.Add(
new
String[] {
"4"
,
"2"
,
"Research"
});
DataTable itemsTable =
new
DataTable();
itemsTable.Columns.Add(
"NodeID"
);
itemsTable.Columns.Add(
"ID"
);
itemsTable.Columns.Add(
"Text"
);
itemsTable.Columns.Add(
"Certificates"
);
itemsTable.Rows.Add(
new
String[] {
"1"
,
"1"
,
"CEO"
,
"MCAD"
});
itemsTable.Rows.Add(
new
String[] {
"1"
,
"2"
,
"CTO"
,
"MCP"
});
itemsTable.Rows.Add(
new
String[] {
"2"
,
"3"
,
"UnitManager"
,
"MCSD"
});
itemsTable.Rows.Add(
new
String[] {
"3"
,
"4"
,
"TeamLead"
,
"MCTS"
});
itemsTable.Rows.Add(
new
String[] {
"4"
,
"5"
,
"TechnicalLead"
,
"MCP"
});
RadOrgChart1.GroupEnabledBinding.NodeBindingSettings.DataFieldID =
"ID"
;
RadOrgChart1.GroupEnabledBinding.NodeBindingSettings.DataFieldParentID =
"ParentID"
;
RadOrgChart1.GroupEnabledBinding.NodeBindingSettings.DataSource = nodeTable;
RadOrgChart1.GroupEnabledBinding.GroupItemBindingSettings.DataFieldNodeID =
"NodeID"
;
RadOrgChart1.GroupEnabledBinding.GroupItemBindingSettings.DataFieldID =
"ID"
;
RadOrgChart1.GroupEnabledBinding.GroupItemBindingSettings.DataTextField =
"Text"
;
RadOrgChart1.GroupEnabledBinding.GroupItemBindingSettings.DataSource = itemsTable;
RadOrgChart1.DataBind();
}
Thanks,
Jayesh Goyani
Thank you both, but this is not working as expected. I have read all samples so the copy of the sample Jayesh is showing does not work.
<%# DataBinder.Eval(Container.DataItem, "name") %> gives no data.
I tried the renderedfields approach, and then I can set the fields, but then they are displayed twice. In all other telerik components when ItemTemplate is present it removes all other data, this does not happen here.
Here is the sample with RenderedField:
<
telerik:RadOrgChart
ID
=
"rocUsers"
runat
=
"server"
DataFieldID
=
"ID"
DataFieldParentID
=
"manager"
DataTextField
=
"fullname"
DisableDefaultImage
=
"False"
GroupColumnCount
=
"2"
>
<
ItemTemplate
>
<
div
style
=
"width: 180px; height: 30px; background-color: Red;"
>
<
asp:Image
ID
=
"Image1"
runat
=
"server"
ImageUrl='<%# "~/images/ownimages/" + ((OrgChartGroupItemRenderer)Container).RenderedFields[0].Text + ".jpg" %>'
Width="30px" Height="30px" />
<%# ((OrgChartGroupItemRenderer)Container).RenderedFields[1].Text %>
<%# ((OrgChartGroupItemRenderer)Container).RenderedFields[2].Text %>
</
div
>
</
ItemTemplate
>
<
RenderedFields
>
<
ItemFields
>
<
telerik:OrgChartRenderedField
DataField
=
"name"
Label
=
":"
/>
<
telerik:OrgChartRenderedField
DataField
=
"fullname"
Label
=
":"
/>
<
telerik:OrgChartRenderedField
DataField
=
"avdeling_name"
Label
=
":"
/>
</
ItemFields
>
</
RenderedFields
>
</
telerik:RadOrgChart
>
The codebehind -loaded in pageload
protected void SetDataSource()
{
DataTable tblUsers;
using (dsEnderTableAdapters.proc_getObjectsTableAdapter adObjects = new dsEnderTableAdapters.proc_getObjectsTableAdapter())
{
using (dsEnder.proc_getObjectsDataTable dtObjects = adObjects.GetData("user", "basic,settings", "manager,FullName", null, null, null, "customwhere", " AND o." + cot + "= " + cid + " AND o.Userstatus='Enabled' AND o.userType IN (1,2) "))
{
rocUsers.GroupColumnCount = 3;
tblUsers = dtObjects;
}
}
rocUsers.DataSource = tblUsers;
rocUsers.DataBind();
}
The data the SQL is returing
id name FullName department Manager
17 aa03 Espen Fosshaug Utvikling 31
31 aa22 Andreas Hisdal Administrasjon NULL
The result is attached.
1. The Itemtemplate does not replace the standard box, it is created inside the box!
2. The renderedfields are displayed after the itemtemplate
Maybe I misunderstand something according to what data is available.
Please advice.
Regards Espen.
For duplicate value issue or The renderedfields are displayed after the itemtemplate.
please comment below code.
<
ItemFields
>
<
telerik:OrgChartRenderedField
DataField
=
"name"
Label
=
":"
/>
<
telerik:OrgChartRenderedField
DataField
=
"fullname"
Label
=
":"
/>
<
telerik:OrgChartRenderedField
DataField
=
"avdeling_name"
Label
=
":"
/>
</
ItemFields
>
Thanks,
Jayesh Goyani
That does not work - if I comment them out they are not rendered and <%# ((OrgChartGroupItemRenderer)Container).RenderedFields[1].Text %> fails with index error.
Espen
There currently are 2 problems that we already have spotted and logged, which are causing the issues you experience.
#1 is that when Simple Binding is used GroupItems doesn't get a DataItem applied. That is why the Container.DataItem doesn't work in Espen's case, and works in Jayesh's example - Espen uses Simple Binding and Jayesh uses Group-Enabled Binding (#1 doesn't reproduce here).
#2 - RenderedFields are rendered even when template is present - Espen, your observation on RadControls is correct - the template should not contain any of the default rendered data.
Please, accept our sincere apologies for the inconvenience caused.
We will do our best to fix the problems as soon as possible and will let you know the moment we are ready in this same support thread.
Regards,
the Telerik team
I hope that you'll be happy to learn, that we successfully have fixed the two problems.
You will be able to get the change in the next Latest Internal Build (should be rolled out later this week).
Just one suggestion that I forgot to make - when having a template and binding to data, a better way to get to the custom fields from your DataSource, would be to use only binding expressions. Otherwise you will be doing practically the same, but having the RenderedFields duplicating the data which should only be displayed in the template.
I hope this helps.
Nikolay Tsenkov
the Telerik team
Looking forward to it. And yes, using DataBindinng when this works will be my approach.
That was what I was trying first, but then used RenderedFields when that failed.
Regards
Espen