We are noticing that in our Blazor server-side application, users are able to click a button more than once unintentionally. How can I prevent this using options available in Blazor framework?
Anything as simple as changing the text of the button to "processing..." as soon as they click the button will suffice our requirements but I am not sure how to achieve this. Handler for click event takes few seconds to process.
Any pointers?
Dropdown Menu component can be enabled/disabled by giving Disabled property. To disable Dropdown Menu component, the disabled property can be set as true .
To disable elements you should use the disabled attribute. I've modified your code a bit and this will do what you're after. Blazor will automatically add or remove the disabled attribute based on the IsDisabled value. You should use the disabled attribute on your button as well.
You likely have one of two problems or both:
For a long running task you can try the following solution. I have not tested it, mileage may vary.
<button disabled=@IsTaskRunning @onclick="DispatchTask">Submit</button>
@code {
bool IsTaskRunning = false;
async void DispatchTask()
{
IsTaskRunning = true;
await DoLongWork();
IsTaskRunning = false;
StateHasChanged();
}
Task DoLongWork()
{
return Task.Delay(6000);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With