Skip to main content

Data Annotation Attributes

  • 5 minutes to read

DevExpress ASP.NET ASPxFormLayout control supports data annotation attributes. You can apply these attributes to the class to specify validation rules and data display mode, set relationships between classes, etc.

Declaration

class TestClass {        
    [Display(Name = "Check it")]
    [CheckBoxRequired(ErrorMessage = "Error text")]
    public bool CheckBoxField { get; set; }
}
<dx:ASPxFormLayout runat="server" ID="fl">
    <Items>
        <dx:LayoutItem FieldName="CheckBoxField">
            <LayoutItemNestedControlCollection>
                <dx:LayoutItemNestedControlContainer>
                    <dx:ASPxCheckBox runat="server" />
                </dx:LayoutItemNestedControlContainer>
            </LayoutItemNestedControlCollection>
        </dx:LayoutItem>
        ...
    </Items>
</dx:ASPxFormLayout>

Data Display Attributes

Common data attributes supported by DevExpress data controls are as follows.

  • DisplayAttribute - Specifies localizable strings for classes and members.

    public class TestClass {
        [Display(GroupName = "{Tabs}/Contact", Order = 2), DataType(DataType.PhoneNumber)]
        public string Phone { get; set; }
    }
    
  • DisplayColumnAttribute - Specifies the column that the control displays in a table as a foreign-key column.

    [DisplayColumn("Phone")]
    public class Customer {
    ...
    }
    
  • CheckBoxValuesAttribute - Specifies the check box value.

    class TestClass {       
            [Display(Name = "Check it")]
            [CheckBoxValuesAttribute(AllowGrayed = false)]
            [CheckBoxRequired(ValidStateMode = CheckBoxValidStateMode.Checked, ErrorMessage = "Error text")]
            public bool CheckBoxField { get; set; }
    }
    
  • DisplayFormatAttribute - Specifies a data field’s display format.

    public class TestClass {
        [DisplayFormat(DataFormatString = "MMMM/yyyy")]
        public DateTime Date { get; set; }
    }
    
  • EditableAttribute - Specifies whether you can edit a data field.

    public class TestClass {
    [Editable(false)] 
    public int MyInt { get; set; } 
    }
    
  • KeyAttribute - Indicates that a property or a field is a key.

    private class TestClass {  
        [Key]  
        public string ID { get; set; }  
    }
    
  • MaxLengthAttribute - Specifies a property value’s maximum length.

    public class TestClass {
        [MaxLength(5)]
        public string Name { get; set; }
    }
    
  • MinLengthAttribute - Specifies a property value’s minimum length.

    public class TestClass {
        [MinLength(5)]
        public string Name { get; set; }
    }
    
  • PhoneAttribute - Specifies that a data field value is a phone number.

    public class TestClass {
        [Phone] 
        public string PhoneNumber { get; set; }
    }
    
  • RegularExpressionAttribute - Specifies that a data field value should match the specified regular expression.

    public class TestClass {
        [RegularExpression(@"\S(.*)\S", ErrorMessage = "Error Text")]
        public object FirstName;
    }
    
  • UIHintAttribute - Specifies a template or user control to display a data field.

    public class EditorTemplatesData {
        [UIHint("Mileage")]
        [Range(10, 999, ErrorMessage = "Must be between {1} <br /> and {2}")]
        public float Mileage { get; set; }
    }
    

Data Type Attributes

  • BindableTypeAttribute - Specifies whether the data type is bindable.

    [BindableType(IsBindable = true)]
        class TestClass
        {
            ...
        }
    
  • DataTypeAttribute - Specifies the type of a data field’s value.

    public class TestClass {
        [DataType(DataType.PhoneNumber)]
        public string Phone { get; set; }
    }
    
  • EnumDataAttribute - Replaces numeric enumerator values with corresponding declarations.

    public class TestClass
    {  
        [EnumDataType(typeof(TestObject))]  
        public object TestObject { get; set; }  
    } 
    
  • MetadataTypeAttribute - Gets data annotation attributes from the specified class.

    [MetadataType(typeof(EmployeeMetadata))]
    public partial class Employee {
    }
    public class EmployeeMetadata {
        [Browsable(false)]
        public Int32 Id { get; set; }
    }
    
  • ScaffoldColumnAttribute - Specifies whether a class or data column is hidden.

    public class TestClass {
        [ScaffoldColumn(true)] 
        public object ID;
    
        [ScaffoldColumn(false)]  
        public object ProductName;  
    }
    
  • ScaffoldTableAttribute - Specifies whether a class or data table is hidden.

    [ScaffoldTable(true)]
    public class TestClass {
        [Display(Name = "Name")]
        public string Name { get; set; }
        ...
    }
    
  • TimestampAttribute - Specifies a column’s data type as a row version.

    public class TestClass {
        public string Name { get; set; }
        [Timestamp]
        public byte[] CardID { get; set; }
        }
    

Validation Attributes

  • CompareAttribute - Compares the editor’s value with another property.

    class TestClass {
        [DataType(DataType.Password), Display(Name = "Password")]
        public string Password { get; set; }
    
        [DataType(DataType.Password), Compare("Password",
                ErrorMessage = "The password you entered do not match"),
                Display(Name = "Confirm Password")]
        public string ConfirmPassword { get; set; }
    }
    
  • ConcurrencyCheck - Checks a property for optimistic concurrency.

    public class Customer {
        [ConcurrencyCheck]
        public string ContactName { get; set; }
    
        [ConcurrencyCheck]
        public string ContactTitle { get; set; }
    }
    
  • CreditCardAttribute - Validates a field value as a credit card number.

    public class TestClass {
        [CreditCard]
        public string CreditCardNumber { get; set; }
    }
    
  • CheckBoxRequiredAttribute - Specifies that a check box is required.

    class TestClass {       
            [Display(Name = "Check it")]
            [CheckBoxValuesAttribute(AllowGrayed = false)]
            [CheckBoxRequired(ValidStateMode = CheckBoxValidStateMode.Checked, 
    ErrorMessage = "Error text")]
            public bool CheckBoxField { get; set; }
    }
    
  • CustomValidationAttribute - Specifies a custom validation method.

    public class TestClass {
        [CustomValidation(typeof(TestValidator), "ValidateName")] 
        public string Name { get; set; }
    }
    
  • EmailAddressAttribute - Validates a field value as an email address.

    public class TestClass {
        [EmailAddress(ErrorMessage = "InvalidEmailAddress")]
        [Display(Name = "Email")]
        public string Email { get; set; }
    }
    
  • FileExtensionsAttribute - Validates file name extensions.

    public class TestClass {
        [FileExtensions(Extensions=". jpg,. png,. gif")]
        public string FileName { get; set; }
    }
    
  • RangeAttribute - Specifies numeric range constraints for the data field value.

    public class TestClass {
        [Range(1, 10, ErrorMessage = "Custom error text")]
        public object Length;
    }
    
  • StringLengthAttribute - Specifies the maximum and minimum length of characters allowed in a data field.

    public class TestClass  
    {  
        [StringLength(4, ErrorMessage = "Error Text")]  
        public string FirstName;  
    
        [StringLength(10)]  
        public string CompanyName;  
    } 
    
  • RequiredAttribute - Specifies that a data field value is required.

    public class TestClass {
        [Required(ErrorMessage = "Name is required")]
        public string Name { get; set; }
    }
    
  • UrlAttribute - Validates a URL.

    public class TestClass  
    {  
        [Url]  
        public string NavigateUrl;  
    }