InputManager - Validation through a web service

 OrderIDProductQuantityDiscountUnitsInStock 
Page size:
 2122 items in 213 pages
10248 Queso Cabrales 12 022
10248 Singaporean Hokkien Fried Mee 10 026
10248 Mozzarella di Giovanni 5 014
10249 Tofu 9 035
10249 Manjimup Dried Apples 40 020
10250 Jack's New England Clam Chowder 10 085
10250 Manjimup Dried Apples 35 0.1520
10250 Louisiana Fiery Hot Pepper Sauce 15 0.1576
10251 Gustaf's Knäckebröd 6 0.05104
10251 Ravioli Angelo 15 0.0536
Isolate this demo as a stand-alone application
  • About this demo
  • C#
  • VB

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 PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<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" 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

Help Us Improve

Was this example helpful?