Back to Devexpress

ASPxCardView.SettingsEditing Property

aspnet-devexpress-dot-web-dot-aspxcardview-0cd47027.md

latest8.2 KB
Original Source

ASPxCardView.SettingsEditing Property

Provides access to the CardView’s editing settings.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public ASPxCardViewEditingSettings SettingsEditing { get; }
vb
Public ReadOnly Property SettingsEditing As ASPxCardViewEditingSettings

Property Value

TypeDescription
ASPxCardViewEditingSettings

An ASPxCardViewEditingSettings object that contains the CardView’s editing settings.

|

Example

The following example implements custom date validation in Batch Edit mode:

  1. Use the BatchEditCardValidating event to check values on the client.
  2. Use the CardValidating event to check values on the server.
  3. Use the AllowValidationOnEndEdit property to switch between validation modes on the client side. If the variation between the HireDate and BirthDate columns is less than 18, input data is considered invalid and data update is not allowed.
aspx
<script type="text/javascript">
    function OnValidation(s, e) {
        if (!clientChkb.GetChecked()) {
            return;
        }
        var grid = ASPxClientCardView.Cast(s);
        var cellInfo1 = e.validationInfo[grid.GetColumnByField("BirthDate").index];
        var cellInfo2 = e.validationInfo[grid.GetColumnByField("HireDate").index];
        var years = CheckYears(cellInfo1.value, cellInfo2.value);
        if (years == null || years < 18) {
            cellInfo1.isValid = false;
            cellInfo2.isValid = false;
            cellInfo2.errorText = "Invalid difference between Hire Date and Birth Date";
            cellInfo1.errorText = "Invalid difference between Hire Date and Birth Date";
        } else {
            cellInfo1.isValid = true;
            cellInfo2.isValid = true;
        }
    }
    function CheckYears(date1, date2) {
        if (!date1 || !date2)
            return null;
        var msecPerYear = 1000 * 60 * 60 * 24 * 365;
        var years = (date2.getTime() - date1.getTime()) / msecPerYear;
        return years;
    }
</script>
<dx:ASPxCheckBox ID="ASPxCheckBox1" runat="server" ClientInstanceName="clientChkb"
    Checked="true" Text="ClientValidation">
</dx:ASPxCheckBox>
<dx:ASPxCheckBox runat="server" ID="ASPxCheckBox2" AutoPostBack="true" OnCheckedChanged="ASPxCheckBox2_CheckedChanged" Checked="false" Text="Validate on the Save Changes button click"></dx:ASPxCheckBox>
<dx:ASPxCardView ClientInstanceName="grid" DataSourceID="AccessDataSource1" KeyFieldName="EmployeeID" ID="ASPxCardView1" OnCardValidating="ASPxCardView1_CardValidating" runat="server" AutoGenerateColumns="False">
    <SettingsEditing Mode="Batch">
    </SettingsEditing>
    <Columns>
        <dx:CardViewTextColumn FieldName="EmployeeID" ReadOnly="True" />
        <dx:CardViewTextColumn FieldName="LastName" />
        <dx:CardViewTextColumn FieldName="FirstName" />
        <dx:CardViewTextColumn FieldName="Address" />
        <dx:CardViewDateColumn FieldName="BirthDate" />
        <dx:CardViewDateColumn FieldName="HireDate" />
    </Columns>
    <CardLayoutProperties>
        <Items>
            <dx:CardViewCommandLayoutItem HorizontalAlign="Right" ShowNewButton="True" />
            <dx:CardViewColumnLayoutItem ColumnName="EmployeeID" />
            <dx:CardViewColumnLayoutItem ColumnName="LastName" />
            <dx:CardViewColumnLayoutItem ColumnName="FirstName" />
            <dx:CardViewColumnLayoutItem ColumnName="Address" />
            <dx:CardViewColumnLayoutItem ColumnName="BirthDate" />
            <dx:CardViewColumnLayoutItem ColumnName="HireDate" />
            <dx:EditModeCommandLayoutItem HorizontalAlign="Right" />
        </Items>
    </CardLayoutProperties>
    <ClientSideEvents BatchEditCardValidating="OnValidation" />
</dx:ASPxCardView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
    SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Address], [BirthDate], 
        [HireDate] FROM [Employees]"></asp:AccessDataSource>
csharp
using DevExpress.Web;

public partial class _Default : System.Web.UI.Page {
    private bool CheckYears(DateTime hireDate, DateTime birthDate) {
        TimeSpan span = hireDate - birthDate;
        if (span.TotalDays / 365 < 18)
            return false;
        else
            return true;
    }
    void AddError(Dictionary<CardViewColumn, string> errors, CardViewColumn column, string errorText) {
        if (errors.ContainsKey(column)) return;
        errors[column] = errorText;
    }
    protected void ASPxCheckBox2_CheckedChanged(object sender, EventArgs e) {
        ASPxCardView1.SettingsEditing.BatchEditSettings.AllowValidationOnEndEdit = !ASPxCheckBox2.Checked;
    }
    protected void ASPxCardView1_CardValidating(object sender, ASPxCardViewDataValidationEventArgs e) {
        DateTime birthDate = (DateTime)e.NewValues["BirthDate"];
        DateTime hireDate = (DateTime)e.NewValues["HireDate"];
        var result = CheckYears(hireDate, birthDate);
        if (!result) {
            AddError(e.Errors, ASPxCardView1.Columns["BirthDate"], "Invalid difference between Hire Date and Birth Date");
            AddError(e.Errors, ASPxCardView1.Columns["HireDate"], "Invalid difference between Hire Date and Birth Date");
            e.CardError = "Correct validation errors";
        }
        else {
            e.CardError = "Modifications aren't allowed in the online example. </br>If you need to test the data editing functionality, please download the example on your machine and run it locally.";
        }
    }

}
vb
Imports DevExpress.Web

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

    Private Function CheckYears(ByVal hireDate As Date, ByVal birthDate As Date) As Boolean
        Dim span As TimeSpan = hireDate.Subtract(birthDate)
        If span.TotalDays / 365 < 18 Then
            Return False
        Else
            Return True
        End If
    End Function
    Private Sub AddError(ByVal errors As Dictionary(Of CardViewColumn, String), ByVal column As CardViewColumn, ByVal errorText As String)
        If errors.ContainsKey(column) Then
            Return
        End If
        errors(column) = errorText
    End Sub

    Protected Sub ASPxCheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        ASPxCardView1.SettingsEditing.BatchEditSettings.AllowValidationOnEndEdit = Not ASPxCheckBox2.Checked
    End Sub
    Protected Sub ASPxCardView1_CardValidating(ByVal sender As Object, ByVal e As ASPxCardViewDataValidationEventArgs)
        Dim birthDate As Date = CDate(e.NewValues("BirthDate"))
        Dim hireDate As Date = CDate(e.NewValues("HireDate"))
        Dim result = CheckYears(hireDate, birthDate)
        If Not result Then
            AddError(e.Errors, ASPxCardView1.Columns("BirthDate"), "Invalid difference between Hire Date and Birth Date")
            AddError(e.Errors, ASPxCardView1.Columns("HireDate"), "Invalid difference between Hire Date and Birth Date")
            e.CardError = "Correct validation errors"
        Else
            e.CardError = "Modifications aren't allowed in the online example. </br>If you need to test the data editing functionality, please download the example on your machine and run it locally."
        End If
    End Sub

End Class

See Also

SettingsBehavior

Card View

ASPxCardView Class

ASPxCardView Members

DevExpress.Web Namespace