New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Validation

  • Politics
  • Sports
  • Events

This example shows how to validate RadDropDownTree by the use of an asp:CustomValidator. In this case the page could be submitted only if something, that will change the default value of the control, is chosen from the RadDropDownTree. The validatePage() function attached to the ClientValidationFunction validator event checks the count of the entries collection of the DropDownTree and if it is 0, sets the args.IsValid to false.
Keep in mind that the control works with all of the standard ASP.NET validators.

  • DefaultCS.aspx
  • DefaultCS.aspx.cs
  • styles.css
<%@ Page AutoEventWireup="false" CodeFile="DefaultCS.aspx.cs" Inherits="DropDownTree.Examples.Validation.DefaultCS"Language="c#"  %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <div class="demo-container">
        <telerik:RadDropDownTree RenderMode="Lightweight" ID="RadDropDownTree1" runat="server" DataSourceID="EntityDataSource1" DefaultMessage="Please select" CheckBoxes="SingleCheck"
            DataTextField="Text" DataFieldID="id" DataValueField="id" DataFieldParentID="parentId" DefaultValue="0" Width="300px"
            OnClientEntryAdded="triggerValidation" OnClientEntryRemoved="triggerValidation">
        </telerik:RadDropDownTree>
        <telerik:RadButton RenderMode="Lightweight" ID="Button1" Text="Submit" runat="server" AutoPostBack="true" OnClick="Button1_Click" ValidationGroup="submit" />
        <br/>
        <asp:CustomValidator runat="server" ClientValidationFunction="validatePage" ValidationGroup="submit" Display="Dynamic"
            CssClass="error-message" ErrorMessage="You need to select a type or a category before submitting the page."></asp:CustomValidator>
        <asp:EntityDataSource runat="server" ID="EntityDataSource1" ConnectionString="name=TelerikReadOnlyEntities"
            DefaultContainerName="TelerikReadOnlyEntities" EntitySetName="Links">
        </asp:EntityDataSource>
        <script type="text/javascript">
            function triggerValidation() {
                Page_ClientValidate();
            }

            function validatePage(sender, args) {
                var dropDownTree = $find("<%=RadDropDownTree1.ClientID%>");

                if (dropDownTree.get_entries().get_count() == 0) {
                    args.IsValid = false;
                }
                else {
                    args.IsValid = true;
                }
            }
        </script>
    </div>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance