I have created a custom textbox component based on a simple element that implements an IOldValue interface. The interface has a function called SetOldValue() that saves the current value to an OldValue variable when called. I want to call a function SetAllOldValues() in the parent component that iterates through only the immediate child components that implement the IOldValue interface and call the SetOldValue function on each. This is instead of creating a variable for each element and calling the function as that would be cumbersome for busy pages. Something like this pseudocode:
foreach (var control in this.Controls.OfType<IOldValue>()){ control.SetOldValue();}Here's the custom textbox
@implements IOldValue@using Microsoft.AspNetCore.Components@using Microsoft.AspNetCore.Components.Web<input @bind="Value" @bind:event="oninput" />@code { [Parameter] public string Value { get; set; } [Parameter] public EventCallback<object> ValueChanged { get; set; } private async Task OnInput(ChangeEventArgs e) { Value = e.Value!.ToString()!; await ValueChanged.InvokeAsync(Value); } public object OldValue { get; set; } public object CurrentValue { get { return _currentValue; } } public string FriendlyName { get; set; } // Ignore public bool HasChanges { get; set; } // Ignore private string _currentValue; public string ReportDifs() { return String.Empty; } public void SetOldValue() { OldValue = Value; }}Interface:
namespace WebUI.Components.Controls;public interface IOldValue{ object OldValue { get; set; } object CurrentValue { get; } string FriendlyName { get; set; } bool HasChanges { get; set; } string ReportDifs(); void SetOldValue();}The parent page
<br /><div class="container col-12"><div class="row"><div class="col-2"><label>Prefix</label></div><div class="col-4"><MyTextBox @ref="@_prefix" @bind-Value="@_person.Prefix" /></div></div><div class="row"><div class="col-2"><label>First Name</label></div><div class="col-4"><MyTextBox @ref="@_firstName" @bind-Value="@_person.FirstName" /></div></div><div class="row"><div class="col-2"><label>Middle Name</label></div><div class="col-4"><MyTextBox @ref="@_middleName" @bind-Value="@_person.MiddleName" /></div></div><div class="row"><div class="col-2"><label>Last Name</label></div><div class="col-4"><MyTextBox @ref="@_lastName" @bind-Value="@_person.LastName" /></div></div><div class="row"><div class="col-2"><label>Suffix</label></div><div class="col-4"><MyTextBox @ref="@_suffix" @bind-Value="@_person.Suffix" /></div></div></div><br /><button class="btn btn-primary" type="button" @onclick="ButtonClicked">Set Old Values</button>@code { private MyTextBox _prefix = new(); private MyTextBox _firstName = new(); private MyTextBox _middleName = new(); private MyTextBox _lastName = new(); private MyTextBox _suffix = new(); private Person _person = new(); protected override void OnInitialized() { _person = new Person { Prefix = "Mr.", FirstName = "John", MiddleName = "Q", LastName = "Public", Suffix = "Sr." }; //SetAllOldValues(); } private void ButtonClicked(MouseEventArgs e) { SetAllOldValues(); } private void SetAllOldValues() { // Pseudocode - will not work as is. // This logic is what is needed. // foreach (var control in this.Controls.OfType<IOldValue>()) // { // control.SetOldValue(); // } // Will work, but cumbersome _prefix.SetOldValue(); _firstName.SetOldValue(); _middleName.SetOldValue(); _lastName.SetOldValue(); _suffix.SetOldValue(); } private class Person { public string Prefix { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public string Suffix { get; set; } }}