Supply Predefined Values for the String Property Editor Dynamically (ASP.NET Web Forms)
- 3 minutes to read
This topic describes implementation of a custom ASP.NET Web Forms Property Editor that will be used to edit a business object’s CultureCode (locale) property of the String type. The dropdown list of the Property Editor’s control will display the cultures returned by the CultureInfo.GetCultures method.
Note
You can also specify predefined values in the Model Editor using the IModelCommonMemberViewItem.PredefinedValues property. This approach is much simpler because it does not require extra coding, but you will be unable to update the values list dynamically in code in this instance.
The image below shows the resulting Property Editor:
To implement a Property Editor by inheriting from the ASPxPropertyEditor class, override the following methods (see ASPxPropertyEditor):
- Override the CreateEditModeControlCore method to return the ASPxComboBox control using the RenderHelper class’ helper method. Subscribe to the control’s ValueChanged event to call the Property Editor’s EditValueChangedHandler method. This method updates the associated property when the control’s value is changed.
- Override the SetupControl method to populate the control’s dropdown list with items. We use the CultureInfo.GetCultures method to retrieve the cultures installed.
To specify that the implemented Property Editor can be used for the String type properties, the PropertyEditor attribute is applied (see PropertyEditorAttribute):
using System;
using System.Globalization;
using System.Web.UI.WebControls;
using DevExpress.Web;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Web;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Web.Editors.ASPx;
using DevExpress.ExpressApp.Model;
//...
[PropertyEditor(typeof(String), "CultureInfoPropertyEditor", false)]
public class CustomStringEditor : ASPxPropertyEditor {
ASPxComboBox dropDownControl = null;
public CustomStringEditor(
Type objectType, IModelMemberViewItem info) : base(objectType, info) { }
protected override void SetupControl(WebControl control) {
if(ViewEditMode == ViewEditMode.Edit) {
foreach(CultureInfo culture in CultureInfo.GetCultures(
CultureTypes.InstalledWin32Cultures)) {
((ASPxComboBox)control).Items.Add(culture.EnglishName + "(" + culture.Name + ")");
}
}
}
protected override WebControl CreateEditModeControlCore() {
dropDownControl = RenderHelper.CreateASPxComboBox();
dropDownControl.ValueChanged += EditValueChangedHandler;
return dropDownControl;
}
public override void BreakLinksToControl(bool unwireEventsOnly) {
if(dropDownControl != null) {
dropDownControl.ValueChanged -= new EventHandler(EditValueChangedHandler);
}
base.BreakLinksToControl(unwireEventsOnly);
}
}
Apply the EditorAliasAttribute attribute to use the implemented Property Editor for a business object’s CultureCode property:
using DevExpress.ExpressApp.Model;
//...
[EditorAlias("CultureInfoPropertyEditor")]
public String CultureCode {
get { return GetPropertyValue<String>(nameof(CultureCode)); }
set { SetPropertyValue(nameof(CultureCode), value); }
}
Here, the EditorAlias attribute changes the PropertyEditorType property of the Application Model’s IModelMember node, that defines the CultureCode property. Alternatively, you can do it using the Model Editor.
You can also implement IComplexViewItem in this Property Editor. This interface allows the editor to access the XafApplication instance and use an Object Space to load data from an application database.
Tip
You can see the code demonstrated here, along with more examples on custom Property Editors in the Feature Center Demo located in the %PUBLIC%\Documents\DevExpress Demos 24.2\Components\XAF\FeatureCenter.NETFramework.XPO folder, by default, or in the Feature Center demo online.