Data Annotation Attributes
- 2 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.
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; } }
MaxLengthAttribute - Specifies a property value’s maximum length.
public class TestClass { [MaxLength(5)] public string Name { 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; }
Data Type Attributes
DataTypeAttribute - Specifies the type of a data field’s value. Supported for
DataType.Currency
andDataType.Password
types.public class TestClass { [DataType(DataType.Password)] public string Password { get; set; } }
Validation Attributes
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; } }
RangeAttribute - Specifies numeric range constraints for the data field value.
public class TestClass { [Range(1, 10, ErrorMessage = "Custom error text")] public object Length; }
RequiredAttribute - Specifies that a data field value is required.
public class TestClass { [Required(ErrorMessage = "Name is required")] public string Name { get; set; } }