How to: Supply Predefined Values for the String Property Editor Dynamically (ASP.NET)

This topic describes implementation of a custom ASP.NET Web 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:

WebPropertyEditorStandardControl

To implement a Property Editor by inheriting from the ASPxPropertyEditor class, override the following methods (see ASPxPropertyEditor):

  1. 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.
  2. 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), 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 += new EventHandler(EditValueChangedHandler);
        return dropDownControl;
    }
    public override void BreakLinksToControl(bool unwireEventsOnly) {
        if(dropDownControl != null) {
            dropDownControl.ValueChanged -= new EventHandler(EditValueChangedHandler);
        }
        base.BreakLinksToControl(unwireEventsOnly);
    }
}

Apply the ModelDefaultAttribute attribute to use the implemented Property Editor for a business object's CultureCode property:

using DevExpress.ExpressApp.Model;
//...
[ModelDefault("PropertyEditorType", "Solution1.Module.Web.CustomStringEditor")]
public String CultureCode {
   get { return GetPropertyValue<String>("CultureCode"); }
   set { SetPropertyValue("CultureCode", value); }
}

Here, the Custom attribute specifies the PropertyEditorType property of the Application Model's IModelMember node, that defines the CultureCode property. Alternatively, you can do it using the Model Editor.

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 18.2\Components\eXpressApp Framework\FeatureCenter folder, by default, or in the Feature Center demo online.