@foreach (var control in GetControlsToRender())
@@ -1380,16 +1382,60 @@
return;
}
- if (binding.ActionSequence is null)
+ if (binding.CodeHandler is null && binding.ActionSequence is null)
continue;
}
}
- else if (binding.ActionSequence is null)
+ else if (binding.CodeHandler is null && binding.ActionSequence is null)
{
- await ReportCommandErrorAsync($"Control event '{eventKind}' has no command or action sequence.");
+ await ReportCommandErrorAsync($"Control event '{eventKind}' has no command, code handler, or action sequence.");
return;
}
+ if (binding.CodeHandler is not null)
+ {
+ var commandApi = new AdminFormCodeModuleCommandApi(
+ Form,
+ Commands,
+ EffectiveCallbackPolicy,
+ Record,
+ binding.Arguments,
+ runtimeArguments,
+ metadata,
+ Form.ActionSequences,
+ ActionRuntime ?? NullFormActionRuntime.Instance,
+ SetActionFieldValueAsync,
+ ReportCommandErrorAsync,
+ OnBuiltInAction);
+ CodeModuleFormDispatchResult codeResult = await CodeModules.DispatchAsync(
+ binding.CodeHandler,
+ new CodeModuleFormEventDispatchContext(
+ Form.FormId,
+ Form.Name,
+ Form.TableName,
+ eventKind.ToString(),
+ Record,
+ binding.Arguments,
+ runtimeArguments,
+ metadata,
+ commandApi,
+ IsCancelable: false,
+ control.ControlId,
+ control.ControlType));
+
+ if (!codeResult.Succeeded)
+ {
+ if (binding.StopOnFailure)
+ {
+ await ReportCommandErrorAsync(codeResult.Message ?? $"Control event '{eventKind}' code handler failed.");
+ return;
+ }
+
+ if (binding.ActionSequence is null)
+ continue;
+ }
+ }
+
if (binding.ActionSequence is not null)
{
FormEventDispatchResult actionResult = await FormActionSequenceExecutor.ExecuteAsync(
diff --git a/src/CSharpDB.Admin.Forms/Components/Designer/PropertyInspector.razor b/src/CSharpDB.Admin.Forms/Components/Designer/PropertyInspector.razor
index ee78c33d..44228c13 100644
--- a/src/CSharpDB.Admin.Forms/Components/Designer/PropertyInspector.razor
+++ b/src/CSharpDB.Admin.Forms/Components/Designer/PropertyInspector.razor
@@ -92,6 +92,7 @@
Events
diff --git a/src/CSharpDB.Admin.Forms/Models/ControlEventBinding.cs b/src/CSharpDB.Admin.Forms/Models/ControlEventBinding.cs
index c4f21be1..1fe64bd5 100644
--- a/src/CSharpDB.Admin.Forms/Models/ControlEventBinding.cs
+++ b/src/CSharpDB.Admin.Forms/Models/ControlEventBinding.cs
@@ -1,3 +1,4 @@
+using CSharpDB.CodeModules;
using CSharpDB.Primitives;
namespace CSharpDB.Admin.Forms.Models;
@@ -15,4 +16,5 @@ public sealed record ControlEventBinding(
string CommandName,
IReadOnlyDictionary