You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using SDK 8.0.101, blazor server-side and have following razor component:
<divcontenteditable="true"class=@Class style=@Style oninput=@OnDocumentChange>@Document</div>
@code {[Parameter]
public string? Class {get;set;}[Parameter]
public string? Style { get; set;}[Parameter]public string Document {get;set;}=string.Empty;[Parameter]publicEventCallback<string> DocumentChanged {get;set;}protectedoverridevoidOnParametersSet(){if(Document isnull)Document=string.Empty;base.OnParametersSet();}privateasync Task OnDocumentChange(ChangeEventArgsargs){Document= args.Value?.ToString()??string.Empty;await DocumentChanged.InvokeAsync(Document);}}
I have noticed that when I edit contents of div component, OnDocumentChange method is always fired with null value. I went to msdn specs (https://developer.mozilla.org/en-US/docs/Web/API/Element/input_event) and noticed that in actual event the field that carries new content isn't named value, but data. I came with a plan to create my own event args that will match actual property name of the event like this:
using System.Text.Json.Serialization;namespace OakLab.Blazor.Components.JsonEditor;internalclassOnInputEventArgs{[JsonPropertyName("data")]publicstring?Data{get;set;}}
however, when content is changed now my app throws an exception:
Error: System.ArgumentException: Object of type 'Microsoft.AspNetCore.Components.ChangeEventArgs' cannot be converted to type 'OakLab.Blazor.Components.JsonEditor.OnInputEventArgs'.
at System.RuntimeType.CheckValue(Object& value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T](MulticastDelegate delegate, T arg)
at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs, Boolean waitForQuiescence)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am using SDK 8.0.101, blazor server-side and have following razor component:
I have noticed that when I edit contents of div component,
OnDocumentChange
method is always fired withnull
value. I went to msdn specs (https://developer.mozilla.org/en-US/docs/Web/API/Element/input_event) and noticed that in actual event the field that carries new content isn't namedvalue
, butdata
. I came with a plan to create my own event args that will match actual property name of the event like this:however, when content is changed now my app throws an exception:
How do I get oninput event work in blazor?
Beta Was this translation helpful? Give feedback.
All reactions