However the Editor_Validating event handler doesn't appear to fire.
What I'm trying to achieve is to not allow the user to create a new node, or change the text of an existing node to the same value as any other node at the same level; and thus ensuring some level of uniqueness.
6 Answers, 1 is accepted
Thank you for writing.
To validate the value of the currently edited Node, you can use ValueValidating event handler. Here is a sample:
I hope this helps.
the Telerik team
Thank you for contacting Telerik Support.
It is important to note that the TreeViewElement.EditMode also takes part for the ValueValidating event. If you have TreeNodeEditMode.Text, the ValueValidating event is not going to fire at all, because in this mode when the user performs edit, the Text will be edited only. By default the EditMode is TextAndValue and the editor is initialized with the Value property of the RadTreeNode. Cancelling the ValueValidating event will not commit the changes. Could you please specify the exact steps how to reproduce the problem with cancelling the ValueValidating event or get back to me with a sample code snippet, which reproduces the specific behavior? Thank you in advance. I also need to know the exact version that you use. Thus, we would be able to investigate the specific case and suggest an appropriate solution if it is available.
I am looking forward to your reply.
Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).
When ValueValidating fires, inside of there I do error checking. If it fails to validate properly, I set e.Cancel = true which should theoretically cancel the editing.
When the treeView_Edited event then fires, c.Cancel for that event is NOT true. So it acts as though everything validated fine. My question is: What should I be doing inside of the ValueValidating function to be able to display an error message of some kind to the user and force them back to editing the node?
If the user is adding a node and they hit the Escape key, I can catch that in the treeView_Edited function as follows:
System.Diagnostics.Debug.WriteLine("START: envTargetsTreeView_Edited Cancellation detected");
// if we were adding a new node when editing was cancelled, simply delete the node
// otherwise, we're simply going to end the editing mode
if (((EnvTargetNodeData)envTargetsTreeView.SelectedNode.Tag).ObjectGuid == Guid.Empty)
// remove the function pointer to prevent this function from being triggered when removing the node
envTargetsTreeView.Edited -= envTargetsTreeView_Edited;
envTargetsTreeView.Edited += new Telerik.WinControls.UI.TreeNodeEditedEventHandler(this.envTargetsTreeView_Edited);
But e.Cancelled is NOT true, even if in the ValueValidating function I tell it to cancel editing. There aren't any examples anywhere of how to implement validation for the tree nodes. What I'd like to do is display a message box of some kind letting them know the problem because it could be one of several problems. Then when they click "Ok" in the messagebox, take them back into editing mode. If they then cancel, it should revert back to the original value, or remove the node if it was being added.
This is using version 2012.2.912.40 if that helps at all.
Thank you for contacting us.
Cancelling the ValueValidating event via e.Cancel=true will keep the current node value. However, it will close the editor. This event uses TreeNodeValidatingEventArgs, but the Edited event uses TreeNodeEditedEventArgs, which e.Canceled is not related to the e.Cancel from the TreeNodeValidatingEventArgs. If your requirement is to keep the editor active when the validation fails, you can use a custom editor. Here is a sample solution, demonstrating how to enter edit mode if the validation fails:
Our Editing Nodes help article is quite useful about the editing process for the node.
Please do not hesitate to contact us if you have any additional questions.