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

Validation


Category IDProductPriceIn stockParent Category ID
1
Page size:
1 Appetizers    
2 Beverages    
3 Cheese    

The following example demonstrates the validation mechanism(s) which can be used with the RadTreeList control. The validation controls (required valiators/range validators) are added to the related EditFormItem and EditFormInsertItem either declaratively, or programatically depending on the level of the item.

Essentially, the example shows how to properly link the validation controls to the edit or insert controls of the RadTreeList control, as well as how to dynamically enable/disable the validation.

  • DefaultCS.aspx
  • DefaultCS.aspx.cs
<%@ Page Language="C#" Inherits="Telerik.Web.Examples.TreeList.DataEditing.Validation.DefaultCS"CodeFile="DefaultCS.aspx.cs"  %>

<%@ 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>
    <style type="text/css">
        .RadTreeList .rtlEditForm .rfdCheckboxChecked,
        .RadTreeList .rtlEditForm .rfdCheckboxUnchecked
        {
            float: none;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" MinDisplayTime="0"></telerik:RadAjaxLoadingPanel>
    <telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1" LoadingPanelID="RadAjaxLoadingPanel1" CssClass="demo-container">
        <div style="margin-bottom: 20px;">
            <span class="checkbox">
                <asp:CheckBox ID="CheckBox1" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" Text="Enable/disable validation" runat="server" Checked="True"></asp:CheckBox></span>
        </div>
        <br />
        <telerik:RadTreeList RenderMode="Lightweight" ID="RadTreeList1" runat="server" OnNeedDataSource="RadTreeList1_NeedDataSource" ParentDataKeyNames="ParentID" DataKeyNames="ID" AllowPaging="true" PageSize="5" OnItemCreated="RadTreeList1_ItemCreated" AutoGenerateColumns="false" AllowSorting="true" OnInsertCommand="RadTreeList1_InsertCommand" OnUpdateCommand="RadTreeList1_UpdateCommand">
            <Columns>
                <telerik:TreeListBoundColumn DataField="ID" UniqueName="ID" HeaderText="Category ID" ReadOnly="true"></telerik:TreeListBoundColumn>
                <telerik:TreeListTemplateColumn DataField="ProductName" UniqueName="ProductName" HeaderText="Product">
                    <ItemTemplate>
                        <%# Eval("ProductName")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" Text='<%# Bind("ProductName")%>' runat="server"></asp:TextBox>
                       <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Required Field!"
                            Visible='<%# CheckBox1.Checked %>'></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <HeaderStyle Width="300px"></HeaderStyle>
                </telerik:TreeListTemplateColumn>
                <telerik:TreeListBoundColumn DataField="Price" UniqueName="Price" DataFormatString="{0:C2}" HeaderText="Price" DataType="System.Decimal">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </telerik:TreeListBoundColumn>
                <telerik:TreeListCheckBoxColumn DataField="InStock" UniqueName="InStock" HeaderText="In stock" DefaultInsertValue="false">
                    <HeaderStyle Width="60px"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                </telerik:TreeListCheckBoxColumn>
                <telerik:TreeListBoundColumn DataField="ParentID" UniqueName="ParentID" HeaderText="Parent Category ID" ReadOnly="true"></telerik:TreeListBoundColumn>
                <telerik:TreeListEditCommandColumn UniqueName="EditCommandColumn">
                </telerik:TreeListEditCommandColumn>
            </Columns>
            <ValidationSettings CommandsToValidate="PerformInsert, Update" EnableValidation="true"></ValidationSettings>
        </telerik:RadTreeList>
    </telerik:RadAjaxPanel>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance