Audit events
You can define interaction behaviors between controls through events.
All events rely on the source control value to change the properties of the target controls.
There are three types of events:
- SetDataSourceParameter: Changes the data of one or more controls based on the value of the source control
- SetIsVisible: Changes the visibility of one or more controls based on the value of the source control
- SetIsRequired: Requires or does not require the value of one or more controls based on the value of the source control
In general, target controls are defined as any control that has a Name or GroupName indicated in the TargetNames field of the method.
In fact, events allow you to create dependencies between controls and groups of controls while minimizing the amount of code.
Control provides methods to quickly create typed events. These methods add the event to the control's Events list . Finally, the list will be executed when the form is rendered. For example, this 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
});
}
SetDataSourceParameter Event
With this event we can modify the data of a target control based on the current value of the source control (and other previous controls). For example, we can relate two DropDownLists so that the selection of a value in the first (e.g. AreaName) corresponds to the cascading data load in the second (e.g. CategoryName).
The event can be added with the AddEventSetDataSourceParameter method that allows you to specify source (ReferenceNames) and target (TargetNames) controls. We can then pass more source values to the target for fancier solutions (e.g. three cascading DropDownLists).
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(["AreaName"], ["CategoryName"]);
}
}
}
The IsLateDataBinding control's property can be set on target controls to prevent data retrieval at form render time. If IsLateDataBinding is true, data retrieval will occur only when the event is executed.
SetIsVisible Event
With this event, we can make other controls visible based on the value of the source control. For example, we can show or hide certain controls based on an activation flag (for example, the optional validity of a news item).
The event can be added with the AddEventSetIsVisible method which allows you to specify the type of condition (IsEmpty, IsNotEmpty,
IsEqual, IsNotEqual), the reference values (ReferenceValues), and the Names or GroupNames of the target controls (TargetNames).
You can also specify the isKeepValueOnHidden option that signals target controls to keep the value when they are 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, ["True"], ["TermTime"]);
}
}
}
SetIsRequired Event
With this event, we can make other controls mandatory based on the value of the source control. For example, we can make a date mandatory if the validity option is set.
The event can be added with the AddEventSetIsRequired method which allows you to specify the type of condition (IsEmpty, IsNotEmpty,
IsEqual, IsNotEqual), the reference values (ReferenceValues), and the Names or GroupNames 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, ["True"], ["TermTimeDateStart"]);
AddEventSetIsRequired(EventCondition.IsEqual, ["True"], ["TermTimeDateEnd"]);
}
}
}