Back to Devexpress

ASPxEdit.Validation Event

aspnet-devexpress-dot-web-dot-aspxedit-dd72c689.md

latest8.0 KB
Original Source

ASPxEdit.Validation Event

Allows you to specify whether the value entered into the editor is valid, and whether the editor is allowed to lose focus.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event EventHandler<ValidationEventArgs> Validation
vb
Public Event Validation As EventHandler(Of ValidationEventArgs)

Event Data

The Validation event's data class is ValidationEventArgs. The following properties provide information specific to this event:

PropertyDescription
ErrorTextGets or sets the error description.
IsValidGets or sets a value specifying whether the validated value is valid.
ValueGets or sets the currently validated value.

Remarks

The Validation event is raised when it is required to validate the editor’s value (for instance, when a value is selected within an editor’s dropdown window or an editor is about to lose focus).

Handle the Validation event to test the editor’s edit value against custom validation criteria. If the entered value obtained via the ValidationEventArgs.Value property doesn’t meet your restrictions, you can prevent the editor from accepting this value (and losing focus) by setting the event parameter’s ValidationEventArgs.IsValid property to false. Then, end users will be forced to correct the edited value. For example, you can check whether the entered text contains only appropriate characters or a numeric value falls into a predefined range, etc.

Additionally, you can provide a text explaining why the validation has failed by assigning a descriptive text to the ValidationEventArgs.ErrorText property. When the ValidationEventArgs.IsValid property is false, the specified text will be displayed within a specific message box.

Example

This example demonstrates how to validate editors in a container on the server side. The ASPxEdit.ValidateEditorsInContainer method is used to determine the validity of editors within a callback panel.

View Example

aspx
<title>How to validate editors in container </title>

<script type="text/javascript" language="javascript">
    function Validate(s, e) {
        if (ASPxClientEdit.ValidateGroup('testGroup'))
            ClientCallbackPanelDemo.PerformCallback('');
    }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <dx:ASPxButton ID="ASPxButtonSave" runat="server" Text="Validate" AutoPostBack="False">
            <ClientSideEvents Click="Validate" />
        </dx:ASPxButton>
        

        <dx:ASPxCallbackPanel ID="ASPxCallbackPanelDemo" runat="server" HideContentOnCallback="False"
            ClientInstanceName="ClientCallbackPanelDemo" OnCallback="ASPxCallbackPanelDemo_Callback">
            <PanelCollection>
                <dx:PanelContent ID="PanelContent1" runat="server">
                    <table cellspacing="0" cellpadding="4" runat="server" id="serverContainer">
                        <tr>
                            <td style="width: 60px;">
                                <dx:ASPxLabel runat="server" ID="NameLabel" AssociatedControlID="txtNum1" Text="Number 1:" />
                            </td>
                            <td>
                                <dx:ASPxTextBox ID="txtNum1" runat="server" Width="170px" OnValidation="ASPxTextBoxTest_Validation">
                                    <ValidationSettings ValidationGroup="testGroup">
                                        <RequiredField IsRequired="True" ErrorText="Number 1 is required" />
                                    </ValidationSettings>
                                </dx:ASPxTextBox>
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 60px;">
                                <dx:ASPxLabel runat="server" ID="ASPxLabel1" AssociatedControlID="txtNum2" Text="Number 2:" />
                            </td>
                            <td>
                                <dx:ASPxTextBox ID="txtNum2" runat="server" Width="170px" OnValidation="ASPxTextBoxTest_Validation">
                                    <ValidationSettings ValidationGroup="testGroup">
                                        <RequiredField IsRequired="True" ErrorText="Number 2 is required"/>
                                    </ValidationSettings>
                                </dx:ASPxTextBox>
                            </td>
                        </tr>
                    </table>
                </dx:PanelContent>
            </PanelCollection>
        </dx:ASPxCallbackPanel>
    </div>
    </form>
</body>
</html>
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxCallbackPanel;

public partial class _Default : System.Web.UI.Page {

    protected void Page_Init (object sender, EventArgs e) {

    }

    protected void ASPxCallbackPanelDemo_Callback (object sender, DevExpress.Web.CallbackEventArgsBase e) {
        ASPxCallbackPanel callbackPanel = (ASPxCallbackPanel)sender;
        bool isValid = ASPxEdit.ValidateEditorsInContainer(callbackPanel);
    }
    protected void ASPxTextBoxTest_Validation (object sender, ValidationEventArgs e) {
        ASPxTextBox txt = sender as ASPxTextBox;
        int val;
        bool result = Int32.TryParse(txt.Text, out val);
        e.IsValid = result;
        e.ErrorText = "An input value should be a number";
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web.ASPxEditors
Imports DevExpress.Web.ASPxCallbackPanel

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub ASPxCallbackPanelDemo_Callback(ByVal sender As Object, ByVal e As DevExpress.Web.CallbackEventArgsBase)
        Dim callbackPanel As ASPxCallbackPanel = CType(sender, ASPxCallbackPanel)
        Dim isValid As Boolean = ASPxEdit.ValidateEditorsInContainer(callbackPanel)
    End Sub
    Protected Sub ASPxTextBoxTest_Validation(ByVal sender As Object, ByVal e As ValidationEventArgs)
        Dim txt As ASPxTextBox = TryCast(sender, ASPxTextBox)
        Dim val As Integer
        Dim result As Boolean = Int32.TryParse(txt.Text, val)
        e.IsValid = result
        e.ErrorText = "An input value should be a number"
    End Sub
End Class

See Also

Validation

Validation

How to: Implement a Custom Validation

ASPxEdit Class

ASPxEdit Members

DevExpress.Web Namespace