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

Reference to the node template in the NodeDataBound handler

7 Answers 109 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Paul Robertson
Top achievements
Rank 1
Paul Robertson asked on 26 Jul 2011, 07:54 PM
Hi there

I have a RadTreeView which binds to a HierarchicalDataSource fine, and I can instantiate a template for the node OK too. The template consists of a RadAjaxPanel that contains an ASCX user control. This all works fine too. My problem is I want to databind a Label web control in the templates' user control to the Content field of the dataitem for each node.

Because the dataitem is only available in the NodeDataBound handler, I reckon I need to get a reference to the Label in the NodeDataBound handler. The problem is, the NodeTemplate property is 'Nothing' when I examine the properties of e.Node from the event arguments. Therefore I don't think I can get a reference to controls in the template at this stage.

Can someone advise, how should I get a reference to the node template at the same time as I can get to the nodes DataItem - the two never seem to be populated at the same time.

Thanks if you can help.

Regards
 

7 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 27 Jul 2011, 03:07 PM
Hi Paul,

Here is a sample demo project that shows how you can get the DataItem in the NodeTemplate.

All the best, Plamen Zdravkov
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Paul Robertson
Top achievements
Rank 1
answered on 28 Jul 2011, 09:46 AM
Hi there

Thanks for getting back to me, but unfortunately I think there is a misunderstanding with my question. I can access DataItem fine in the NodeDatabound handler, in the way demonstrated in the example you provide - however, as I mentioned before, what I am struggling to work out is how to get a reference into the controls of the ASCX based node template, in that same NodeDatabound handler. The example you provide just demonstrates the basic example of creating Label objects in the InstantiateIn method. I am wondering what is the recommended technique for databinding the DataItem contents into a Label defined in the ASCX control that I am using for the template. Currently, the NodeTemplate property appears to be empty in the NodeDatabound handler. Is NodeTemplate supposed to be Nothing in this handler? The template appears to be instantiated OK, it is used in the rendering of each RadTreeNode, it's just that I cannot databind individual Label controls in the ASCX control. I reckon the best way would be, in the NodeDataBound handler, to access my DataItem object (e.Node.DataItem), no problem here, but then how to get a reference to the Template controls in this handler, to then set the Text property etc. for template controls:

Foobar example:
FoobarLabel.Text = e.Node.DataItem.Content

The right side of this works for me already OK, I have a custom data object with String properties, BUT ... for the left side, how do I find the Label control if I cannot get a reference to the node template in NodeDatabound.

Thanks for any advice.

Regards



 
0
Plamen
Telerik team
answered on 29 Jul 2011, 02:13 PM
Hello Paul,

If I understood you correctly, you want to bind a NodeTemplate which is in the ASPX page-here is the modified project.

If you are trying to get the NodeTemplate in the NodeBataBound event you can do it with RadTreeView1.NodeTemplate (in this example) because the e.Node. NodeTemplate is actually the Per-Node Template, but in this case there are no Per-Node Templates.


All the best, Plamen Zdravkov
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Paul Robertson
Top achievements
Rank 1
answered on 29 Jul 2011, 03:44 PM
Hello

Thanks, that makes sense, but what is the recommended technique for accessing the contents of the per-node template in that case? In the end, I just had to use a rather clumsy Find process in the NodeDataBound handler, that depended on my providing the type I was looking for:

            For Each ctrl1 As Control In e.Node.Controls
                If ctrl1.GetType.ToString = "Telerik.Web.UI.RadAjaxPanel" Then
                    For Each ctrl2 As Control In ctrl1.Controls
                        If ctrl2.GetType.ToString = "ASP.MyFooBarCommentUserControlType" Then
                            currentUC = CType(ctrl2, ASP.MyFooBarCommentUserControlType)
                        End If
                    Next
                End If
            Next

But this seemed a bit inelegant, so I am wondering if the controls within per-node templates cannot be accessed in a more efficient way.

Thanks very much if you can advise.

Regards

0
Plamen
Telerik team
answered on 03 Aug 2011, 02:39 PM
Hi Paul,

Unfortunately the per-node templates are not available in the NodeDateBound.

You can have a look at our ASPX Template Structure help topic for more information about them.
 
Greetings,
Plamen Zdravkov
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Paul Robertson
Top achievements
Rank 1
answered on 04 Aug 2011, 09:16 AM
I had to use a user control in a default tree-wide 'node template' in the end, using the GetType find method indicated above, as it appears it is impossible to programmatically access the user control, or indeed any template controls at all, for each 'per node template' in the NodeDataBound handler. Although this seems a  strange limitation to encounter, I would happily be proved wrong it somebody has found a way to do it. The link in the previous response is just a link to the standard documentation, which doesn't address the incorporation of user controls, although in this case it doesn't appear to matter how you construct your 'per-node' template, as only the default node template can be manipulated at NodeDataBound, not the per-node template.
0
Plamen
Telerik team
answered on 09 Aug 2011, 03:25 PM
Hello Paul,

It truly looks like a limitation of the template usage and that is why i sent you the link that explains what exactly is the usage of the per-node template and the when the nodes are generated one can only use the Global ones.

Greetings,
Plamen Zdravkov
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
TreeView
Asked by
Paul Robertson
Top achievements
Rank 1
Answers by
Plamen
Telerik team
Paul Robertson
Top achievements
Rank 1
Share this question
or