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

RadTreeView - finding control generated by ITemplate

1 Answer 159 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 30 Jul 2008, 07:58 PM
I have 2 buttons on the page and a treeview. One button is Edit and the other is Save.

The treeview is generated dynamically by adding nodes using ITemplate...for example, some nodes add a RadTextBox or a RadDatePicker

When the user clicks Edit I dynamically reload the entire tree with each node using a different ITemplate showing the edit controls.

When the user clicks Save I want to get each node and find the control holding the edited value. The problem is Node.Controls is empty. I can't do a FindControl either on any container in the chain.

I've tried:

var thisControl = node.FindControl(node.UniqueID + "dateField");
var thisControl = node.Parent.FindControl(node.UniqueID + "dateField");
var thisControl = this.FindControl(node.UniqueID + "dateField");

Here is the Save event....

protected void dataElementFactTree_Save_Click(object sender, ImageClickEventArgs e)

{

var tree = ((ImageButton) sender).FindControl("dataElementFactTree") as RadTreeView;

if (tree == null) return;

ProcessTreeNodes(tree);

}

private void ProcessTreeNodes(IRadTreeNodeContainer tree)

{

var valueActions = new List<BGValueAction>();

foreach (RadTreeNode node in tree.Nodes)

{

var nodeTemplate = node.Attributes["NodeTemplate"];

if (nodeTemplate == typeof(CalendarTemplate).ToString())

{

var thisControl = node.FindControl(node.UniqueID + "dateField");

}

{

};

valueActions.Add(valueAction);

}

if (valueActions.Count > 0) BGMainController.ProcessFactTreeActions(valueActions);

}

Here is a sample of a read and edit template:

internal class CalendarTemplate : BaseTemplate, ITemplate

{

#region ITemplate Members

public CalendarTemplate(NodeBuilderContext context, bool isEditing) : base(context, isEditing, false) { }

public void InstantiateIn(Control container)

{

var node = (RadTreeNode) container;

CreateLabel(container);

if (IsEditing)

{

var factValueField = new RadDatePicker {ID = container.UniqueID + "dateField"};

node.Controls.Add(factValueField);

}

else

{

CreateValueLabel(container,

string.Empty);

}

}

#endregion

}

Thanks.

1 Answer, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 31 Jul 2008, 02:20 PM
Hi Scott,

I assume that in your case you load the Templates only when the Edit button is clicked. Is that correct? In this case, after you click the Save button, the Templates are not added, hence the controls are missing. This implicates that the data in these controls is also lost.

Templates should be added on every postback and before the Nodes in the TreeView are initialized. The most suitable place for doing this is the OnInit event handler of the page. Please see this help topic for more information.

Finally, to workaround the problem you can modify the Templates a little bit: Add the Templates on every postback but hide the controls by default and show them only if the Edit button has been clicked.

Best wishes,
Simon
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
TreeView
Asked by
Scott
Top achievements
Rank 1
Answers by
Simon
Telerik team
Share this question
or