简体   繁体   English

在提交表单之前,如何检测 Blazor Server EditForm 中的更改

[英]How can I detect a change in a Blazor Server EditForm before the form is submitted

Essentially, what I'm trying to do, is have the 'Update' button within an EditForm appear disabled, until the user has made a change to the text contained within a textbox.本质上,我正在尝试做的是让 EditForm 中的“更新”按钮显示为禁用状态,直到用户对文本框中包含的文本进行了更改。

<EditForm EditContext="@EditContext">
    <InputText @bind-Value="SomeModel.Text></InputText>
    ...
<EditForm/>

I've set up the event handler as follows我已经按如下方式设置了事件处理程序

private EditContext EditContext { get; set; }
protected override void OnInitialized()
{
    this.EditContext = new EditContext(this.SomeModel);
    EditContext.OnFieldChanged += FieldChanged;
}

This works, but not in the way I want it to.这有效,但不是我想要的方式。 With this I have to wait until the user changes focus before the event fires, but I want it to fire as soon as the text changes (The equivalent of WinForms TextBox.OnTextChanged)有了这个,我必须等到用户在事件触发之前改变焦点,但我希望它在文本更改时立即触发(相当于 WinForms TextBox.OnTextChanged)

For reference, you can create your own versions of the Input controls very easily.作为参考,您可以非常轻松地创建自己的输入控件版本。 Here's a version of the text version where you can set the update event:这是您可以在其中设置更新事件的文本版本:

public class BlazrInputText : InputText, IComponentReference
{
    [Parameter] public bool BindOnInput { get; set; } = true;

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenElement(0, "input");
        builder.AddMultipleAttributes(1, AdditionalAttributes);

        if (!string.IsNullOrWhiteSpace(this.CssClass))
            builder.AddAttribute(2, "class", CssClass);

        builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValueAsString));

        if (BindOnInput)
            builder.AddAttribute(4, "oninput", EventCallback.Factory.CreateBinder<string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
        else
            builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder<string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString));

        builder.AddElementReferenceCapture(6, __inputReference => Element = __inputReference);
        builder.CloseElement();
    }
}

3rd party libraries usually have this implemented in their textbox controls but since you're using the existing Blazor InputText control, Microsoft shared a way to use oninput with InputText by making your own custom Text control as shown here . 3rd 方库通常在其文本框控件中实现了此功能,但由于您使用的是现有的 Blazor InputText 控件,Microsoft 通过制作您自己的自定义 Text 控件共享了一种将oninput与 InputText 一起使用的方法, 如下所示

Use the InputText component to create a custom component that uses the oninput event (input) instead of the onchange event (change).使用 InputText 组件创建一个使用 oninput 事件(输入)而不是 onchange 事件(更改)的自定义组件。 Use of the input event triggers field validation on each keystroke使用输入事件会在每次击键时触发字段验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM