Edit this page

Validate Treeview by Checked Nodes

By design, the RequiredFieldValidator can validate RadTreeView for a selected node.

This article shows two methods of validating RadTreeView for a checked nodes. In this way, you will be sure that there is at least one checked node of your TreeView.

Using the RequiredFieldValidator

In order this to work, you need to select/unselect the node on its checking/unchecking.

Here is the relevant code:

<telerik:RadTreeView RenderMode="Lightweight" ID="RadTreeView1" runat="server" CheckBoxes="true" OnClientNodeChecked="OnClientNodeCheckedHandler"
    Skin="Sunset">
</telerik:RadTreeView>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RadTreeView1"
    ErrorMessage="Please check at least one node">
</asp:RequiredFieldValidator>
function OnClientNodeCheckedHandler(sender, eventArgs) {
    var node = eventArgs.get_node();
    node.set_selected(node.get_checked());
}   

Using the CustomValidator

The custom validator will check if the array containing the checked nodes has at least one node.

<telerik:RadTreeView RenderMode="Lightweight" ID="RadTreeView2" runat="server" CheckBoxes="true" Skin="Hay">
</telerik:RadTreeView>

<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validateTreeViewCheckboxes"
    ErrorMessage="Please check at least one node" OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
function validateTreeViewCheckboxes(source, args) {
    var tree = $find("<%= RadTreeView2.ClientID %>");
    args.IsValid = tree.get_checkedNodes().length > 0;
}   
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 
{ 
    args.IsValid = RadTreeView2.CheckedNodes.Count > 0; 
}
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
    args.IsValid = RadTreeView2.CheckedNodes.Count > 0
End Sub

See Also