Control events

It's possible to define interaction behaviors between controls through events.
All events are based on the value of the source control to modify the properties of target controls.
 
There are three types of events:
  • SetDataSourceParameter: modifies the data of one or more controls based on the value of the source control
  • SetIsVisible: modifies the visibility of one or more controls based on the value of the source control
  • SetIsRequiredrequires or doesn't require the value of one or more controls based on the value of the source control
 
In general, target controls refer to those with a Name or GroupName specified in the TargetNames field of the method.
In fact, events allow creating dependencies between controls and control groups while minimizing the amount of code.
 
ControlBase provides methods to quickly create typed events. These methods add the event to the Events list of the control. The list will be executed during the form rendering phase. Here, for example, is the implementation of AddEventSetIsVisible:
 
public void AddEventSetIsVisible(EventCondition condition, List<string> referenceValues, List<string> targetNames, bool isKeepValueOnHidden = true)
        {
            this.Events.Add(new Event()
            {
                Type = EventType.SetIsVisible,
                Condition = condition,
                ReferenceValues = referenceValues,
                TargetNames = targetNames,
                IsKeepValueOnHidden = isKeepValueOnHidden
            });
        }
 
Event SetDataSourceParameter
With this event, we can modify the data of a target control based on the current value of the source control (and other preceding controls). For example, we can establish a relationship between two DropDownList controls so that selecting a value in the first one (e.g., AreaName) corresponds to loading cascading data in the second one (e.g., CategoryName).
The event can be added using the AddEventSetDataSourceParameter method, which allows specifying source controls (ReferenceNames) and target controls (TargetNames). This way, we can pass multiple source values to the target for more advanced solutions (such as three cascading DropDownList controls).
 
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using DataWeb.Data;
using DataWeb.Data.Controls;
using DataWeb.WebApp.Infrastructure;
using Microsoft.Extensions.Localization;

namespace MyApp.DataWeb.Data.Controls
{
    public class Product_AreaName :  DropDownList
    {
        private readonly AreaRepository areaRepository;
        private readonly IStringLocalizer localizer;

        public Product_AreaName(Form form, IServiceProvider serviceProvider) : base(form, serviceProvider)
        {
            areaRepository = serviceProvider.GetService<AreaRepository>();
            localizer = serviceProvider.GetService<IStringLocalizer>();
        }

        public override async Task InitAsync()
        {
            await base.InitAsync();

            AddEventSetDataSourceParameter(new List<string> { "AreaName" }, new List<string> { "CategoryName" });
        }
    }
}
 
The property IsLateDataBinding of the control can be set on target controls to prevent data retrieval during the form rendering phase. If IsLateDataBinding is true, data retrieval will only occur upon event execution.
 
 
Event SetIsVisible
With this event, we can make other controls visible based on the value of the source control. For example, we can show or hide specific controls based on an activation flag (e.g., the optional validity of a news).
The event can be added using the method AddEventSetIsVisible, which allows specifying the condition type (IsEmpty, IsNotEmpty, IsEqual, IsNotEqual), the reference values (ReferenceValues), and the Name or GroupName of the target controls (TargetNames).
Furthermore, you can specify the option isKeepValueOnHidden, which instructs the target controls to retain the value when hidden.
 
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DataWeb.Data;
using DataWeb.Data.Controls;

namespace MyApp.DataWeb.Data.Controls
{
    public class News_IsTermTime :  DropDownList
    {
        public News_IsTermTime(Form form, IServiceProvider serviceProvider) : base(form, serviceProvider)
        {
        }

        public override async Task InitAsync()
        {
            await base.InitAsync();

            AddEventSetIsVisible(EventCondition.IsEqual, new List<string> { "True" }, new List<string> { "TermTime" });
        }
    }
}
 
Event SetIsRequired
With this event, we can make other controls required based on the value of the source control. For example, we can make a date field required if the validity option is set.
The event can be added using the method AddEventSetIsRequired, which allows specifying the condition type (IsEmpty, IsNotEmpty, IsEqual, IsNotEqual), the reference values (ReferenceValues), and the Name or GroupName of the target controls (TargetNames).
 
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DataWeb.Data;
using DataWeb.Data.Controls;

namespace MyApp.DataWeb.Data.Controls
{
    public class News_IsTermTime :  DropDownList
    {
        public News_IsTermTime(Form form, IServiceProvider serviceProvider) : base(form, serviceProvider)
        {
        }

        public override async Task InitAsync()
        {
            await base.InitAsync();

            AddEventSetIsRequired(EventCondition.IsEqual, new List<string> { "True" }, new List<string> { "TermTimeDateStart" });
            AddEventSetIsRequired(EventCondition.IsEqual, new List<string> { "True" }, new List<string> { "TermTimeDateEnd" });
        }
    }
}