RadTreeView Node Click Subclass

1 Answer 67 Views
TreeView
Steven
Top achievements
Rank 1
Iron
Steven asked on 10 Feb 2023, 07:15 AM

Is it possible to subclass the RadTreeNode and get it back on a node click.  In the image attached I've made a class inheriting from RadTreeNode named FileTreeNode.  It kinda works except my custom properties appear but do not have any data.  FIleName is null.

 

Attila Antal
Telerik team
commented on 14 Feb 2023, 11:44 AM

Hi Steven,

By default, the RadTreeView creates RadTreeNodes based on the data source. So I am wondering, in your case, how are the nodes created and added to the TreeView? Is it done automatically by simply binding the Control to data, or you are Creating the TreeNodes and adding them to the treeview's Nodes Collection?

When do you populate the properties FileID, FileName, IsFolder, IsPrivate properties? These things do not get automatically populated by TreeView.

Steven
Top achievements
Rank 1
Iron
commented on 14 Feb 2023, 12:18 PM

I'm manually adding the nodes to the tree.  The nodes I'm adding, FileTreeNode, inherit from RadTreeNode.  Something like this:

FileTreeNode objFnode = [routine to create a new FileTreeNode]
radTree.Nodes.Add(objFnode);

 

What's funny is that on a server side node click event, I can cast the DataiIem as a FileTreeNode and I can see the properties in the subclass, i.e. IsPrivate, etc.  but there is not data in the subclassed fields.

FileTreeNode objNodeD = (FileTreeNode)e.Node.DataItem;  /// no error but but no data

 

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 17 Feb 2023, 08:53 AM

Hi Steven,

It is not enough that you add the Node to the TreeView. You will also need to populate them, otherwise, there is no way they will contain values.

Here is an example of doing that

Markup

<telerik:RadTreeView ID="RadTreeView1" runat="server" OnNodeClick="RadTreeView1_NodeClick"></telerik:RadTreeView>

<asp:Label ID="lblMessage" runat="server"></asp:Label>

 

C#

protected void Page_Init(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        // Populate the properties
        RadTreeView1.Nodes.Add(new FileTreeNode() { FileID = i, Text= "Text "+i, Value = "Value" + i });
    }
}

protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
{
    FileTreeNode ftNode = (FileTreeNode)e.Node;

    lblMessage.Text = string.Format("Node with FileID {0} was clicked!", ftNode.FileID);
}

public class FileTreeNode : RadTreeNode
{
    public int FileID { get; set; }
    public FileTreeNode() : base()
    {

    }
}

 

Result

 

 

Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Steven
Top achievements
Rank 1
Iron
commented on 17 Feb 2023, 06:36 PM


private FileTreeNode makeFolderNode(DealRoomFilesEntity objFile)
{
	FileTreeNode rfNode = new FileTreeNode(objFile.FolderName, fileName: "", (int)objFile.FolderID, isFolder: true, isPrivate: false);
	rfNode.Expanded = objFile.FolderFileCount > 0;
	return rfNode;
}
I am populating the FileTreeNode with all the data, see the routine above. The returned object is then added to the tree. I stepped through it with a debugger.  The RadTreeView only retains it's native data, not the subclassed data for node clicks.  I have figured out a workaround, I was just curios if it was a bug or by design.
Attila Antal
Telerik team
commented on 20 Feb 2023, 08:43 AM

Hi Steve, 

Inheriting Objects is part of .NET Framework and if there is a problem there, the issue/bug would be with the Framework.

In my test sample data is available. I populated the TreeView with Custom Node objects and those are available later. You can test the code I shared above.

Steven
Top achievements
Rank 1
Iron
commented on 20 Feb 2023, 01:31 PM

Thank you for the code Attila.  You code worked in my environment so I'll have to figure out why mine does not work.  
Tags
TreeView
Asked by
Steven
Top achievements
Rank 1
Iron
Answers by
Attila Antal
Telerik team
Share this question
or