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

Validation through a web service

 OrderIDProductQuantityDiscountUnitsInStock 
Page size:
 2155 items in 216 pages
11072 Wimmers gute Semmelknödel 130 022
11073 Queso Cabrales 10 022
11073 Guaraná Fantástica 20 020
11074 Pavlova 14 0.0529
11075 Chang 10 0.1517
11075 Spegesild 30 0.1595
11075 Lakkalikööri 2 0.1557
11076 Grandma's Boysenberry Spread 20 0.25120
11076 Tofu 20 0.2535
11076 Teatime Chocolate Biscuits 10 0.2525

This example demonstrates RadInputManager's capability to validate the controls it extends through a web service. In this particular scenario, detailed order information can be added or changed only if the specified quantity is not greater than the units in stock of the corresponding product.

In order to use RadInputManager's validation-through-a-web-service feature, you need to:

  • define a WebService class and apply to it the ScriptServiceAttribute;
  • implement a WebMethod that accepts two or three parameters and returns a bool value, indicating whether the validation has passed or failed:
    • the first parameter to the method must be a string object and contains the id of the control being validated;
    • the second parameter also needs to be a string object and holds the value of the control being validated;
    • the third parameter is optional and of type object - it is used to transfer to the validation method any additional information that might be needed for the validation logic. This parameter can be set in the event arguments object passed to the OnValidating event which is raised both on the client and on the server before the web service validation method has been called.
  • specify the path to the WebService through the corresponding RadInputManager setting's Location property and the validation method through the Method property;
  • indicate the event on which validation should be performed through the corresponding RadInputManager setting's ValidateOnEvent property.

By analogy to the MS ASP.NET CustomValidator control RadInputManager performs validation both on the client and on the server with the server-side validation being executed only if the client one has passed, has been disabled or JavaScript switched off in the browser.

In this example the validation condition on the client is the same as the one on the server but in other real-world scenarios there might be cases when the validation logic on the client may differ from that on the server.

The following example demonstrates RadInputManager's validation through a web service. Detailed order information can be added or changed only if the specified quantity is not greater than the units in stock of the corresponding product.

Related Resources

  • DefaultCS.aspx
  • DefaultCS.aspx.cs
    • DefaultCS.aspx.cs
    • inputmanagervalidationservice.cs
  • scripts.js
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultCS.aspx.cs" Inherits="Input_Examples_RadInputManager_DynamicInputFilterSettings_DefaultCS" %>

<%@ 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>
    <script src="scripts.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    <telerik:AjaxUpdatedControl ControlID="RadInputManager1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>

    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
    <div>
        <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
            <script type="text/javascript">
                function pageLoad() {
                    window.$ = jQuery = $telerik.$;
                }
            </script>
        </telerik:RadScriptBlock>

        <telerik:RadInputManager RenderMode="Lightweight" ID="RadInputManager1" OnValidating="RadInputManager1_Validating"
            runat="server">
            <telerik:TextBoxSetting>
                <TargetControls>
                    <telerik:TargetInput ControlID="RadGrid1" />
                </TargetControls>
            </telerik:TextBoxSetting>
            <telerik:NumericTextBoxSetting BehaviorID="TextBoxBehavior1" Type="Number" DecimalDigits="0"
                InvalidStyleDuration="2000">
                <Validation Location="validationservice/InputManagerValidationService.asmx" ValidateOnEvent="Submit"
                    Method="ValidateQuantity" />
                <ClientEvents OnValidating="onValidating" OnError="onError" />
            </telerik:NumericTextBoxSetting>
        </telerik:RadInputManager>

        <div class="demo-container no-bg">
            <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowMultiRowEdit="true"
                GridLines="None" AllowPaging="True"
                OnUpdateCommand="RadGrid1_UpdateCommand" OnItemCreated="RadGrid1_ItemCreated"
                AllowAutomaticUpdates="true" AllowAutomaticInserts="true" AllowAutomaticDeletes="true"
                AllowSorting="true">
                <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID, ProductID" DataSourceID="SqlDataSource1">
                    <PagerStyle Mode="NextPrevAndNumeric" />
                    <Columns>
                        <telerik:GridEditCommandColumn ButtonType="FontIconButton" />
                        <telerik:GridTemplateColumn DataField="OrderID" DataType="System.Int32" HeaderText="OrderID" SortExpression="OrderID"
                            UniqueName="OrderID">
                            <ItemTemplate>
                                <asp:Label ID="idLabel" runat="server" Text='<%#Eval("OrderID") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox runat="server" Enabled="false" ID="orderTxt" Text='<%#Bind("OrderID") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <InsertItemTemplate>
                                <telerik:RadComboBox RenderMode="Lightweight" ID="idCombo" runat="server" CausesValidation="true"
                                    MarkFirstMatch="true" DataSourceID="SqlDataSource3" DataTextField="OrderID" DataValueField="OrderID"
                                    MaxHeight="200px" SelectedValue='<%#Bind("OrderID") %>'>
                                </telerik:RadComboBox>
                            </InsertItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridDropDownColumn DataField="ProductID" DataSourceID="SqlDataSource2" UniqueName="ProductID"
                            HeaderText="Product" ListTextField="ProductName" ListValueField="ProductID" DropDownControlType="RadComboBox">
                        </telerik:GridDropDownColumn>
                        <telerik:GridTemplateColumn DataField="Quantity" UniqueName="Quantity" HeaderText="Quantity"
                            SortExpression="Quantity">
                            <ItemTemplate>
                                <asp:Label ID="quantityLabel" runat="server" Text='<%#Eval("Quantity") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="quantityNumBox" runat="server" Text='<%#Bind("Quantity") %>'>
                                </asp:TextBox>
                                <asp:Label ID="ErrorLabel" runat="server" ForeColor="Red"></asp:Label>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridNumericColumn DataField="Discount" MinValue="0" MaxValue="1" AllowOutOfRangeAutoCorrect="true" DecimalDigits="2" HeaderText="Discount" SortExpression="Discount" UniqueName="Discount">
                        </telerik:GridNumericColumn>
                        <telerik:GridBoundColumn DataField="UnitsInStock" DataType="System.Int32" HeaderText="UnitsInStock"
                            ReadOnly="true" SortExpression="UnitsInStock" UniqueName="UnitsInStock">
                        </telerik:GridBoundColumn>
                        <telerik:GridButtonColumn CommandName="Delete" ButtonType="FontIconButton" />
                    </Columns>
                    <EditFormSettings>
                        <EditColumn ButtonType="FontIconButton" />
                    </EditFormSettings>
                </MasterTableView>
                <ValidationSettings EnableValidation="false" />
            </telerik:RadGrid>
        </div>

        <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString35 %>"
            runat="server" SelectCommand="SELECT OrderID, Products.ProductID, ProductName, Quantity, Discount, UnitsInStock From [Order Details] join Products on [Order Details].ProductID = Products.ProductID"
            ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [Order Details] WHERE [OrderID] = @original_OrderID AND [ProductID] = @original_ProductID"
            OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Order Details] SET [ProductID]=@ProductID, [Quantity] = @Quantity, [Discount] = @Discount WHERE [OrderID] = @original_OrderID AND [ProductID] = @original_ProductID AND [Quantity] = @original_Quantity AND [Discount] = @original_Discount">
            <DeleteParameters>
                <asp:Parameter Name="original_OrderID" Type="Int32" />
                <asp:Parameter Name="original_ProductID" Type="Int32" />
                <asp:Parameter Name="original_Quantity" Type="Int16" />
                <asp:Parameter Name="original_Discount" Type="Single" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Quantity" Type="Int16" />
                <asp:Parameter Name="Discount" Type="Single" />
                <asp:Parameter Name="original_OrderID" Type="Int32" />
                <asp:Parameter Name="original_ProductID" Type="Int32" />
                <asp:Parameter Name="original_Quantity" Type="Int16" />
                <asp:Parameter Name="original_Discount" Type="Single" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString35 %>"
            runat="server" SelectCommand="SELECT ProductID, ProductName from Products WHERE UnitsInStock > 0"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource3" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString35 %>"
            runat="server" SelectCommand="SELECT OrderID FROM Orders "></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance