Skip to content

UI EnableDisableControls

Victor Tomaili edited this page May 3, 2021 · 1 revision

Enable/Disable a control programmatically (from xyzDialog)

Code sample: in Serene/BasicSamples/Dialogs/ReadOnlyDialog

Saltarelle version:

    EditorUtils.SetReadOnly(form.MyField, true)

TypeScript version:

    constructor() {
        super();

        // Add change event to control field
        this.form.EnableField.change(e =>
        {
            if (this.form.EnableField.value == 1)
                // Set read only to true for a field
                Serenity.EditorUtils.setReadOnly(this.form.TestField, true);
                
            if (this.form.EnableField.value == 2)
                // Set read only to false for a field
                Serenity.EditorUtils.setReadOnly(this.form.TestField, false);
        }
   );

Enable/Disable a control and lookup change selection

TypeScript version:

    constructor() {
        super();

        // Add change event to lookup field
        this.form.LookUpField.ChangeSelect2(e =>
        {
            // Set read only to true for a field
            Serenity.EditorUtils.setReadOnly(this.form.TestField, true);
        }
   );

Enable/Disable a control with attribute in XYZForm.cs

For make a field readonly use the ReadOnlyAttribute.

    [FormScript("Test.Test_Table")]
    [BasedOnRow(typeof(Entities.Test_TableRow))]
    public class Test_TableRow
    {
        [ReadOnly(true)]
        public String MyField { get; set; }

If you want to ensure that a field is read-only only in update mode (and editable in insert mode), use the attribute UpdatableAttribute. In this way when the Form is open in insert mode (new) field is editable. When the Form is open in edit mode, the field is read-only.

    [FormScript("Test.Test_Table")]
    [BasedOnRow(typeof(Entities.Test_TableRow))]
    public class Test_TableRow
    {
        [Updatable(false)]
        public String MyField { get; set; }
Clone this wiki locally