How to show a message yes/no before delete record?

Hi, i have create a small app for testing, in the grid radzen have create a button to delete the record, but i need to show a message yes/no before the delete operation.

I read the documentation and i read the post how to use the Condition to insert the confirm('delete this ?').

How to do this from blazor ?

Many Thanks

Aurelio

Same question here....

The Blazor DataGrid does not have built-in delete confirmation support yet. One can use the Blazor JSInterop though.

  1. Open the Page.razor.cs file and add the following code to the page class
         [Inject]
         IJSRuntime JSRuntime { get; set; }
    
         async Task<bool> Confirm(string message)
         {
              var result = await JSRuntime.InvokeAsync<bool>("confirm", new [] { message });
    
              return result;
         }
    
  2. Then in Radzen use the Invoke method action:
  3. Invoke the deleteXXX data source method if the Confirm method returned true.
1 Like

Hi..many tanks for the solution..

By Aurelio

Hello Radzen Team,
are you planning to add a "confirm delete" option on Blazor data grids? And on Button controls?
I use it on every delete action...

Regards
Thomas

We probably will at some point. Not scheduled at the moment though.

I use https://blazor.radzen.com/dialog for that.
Either you can have just a message saying "Are you sure?" or show what will be deleted.
It works very well.

1 Like

Where can i find the click event in datagrid?

Thanks for help
Thomas

I use visual studio so I am not familiar with the Radzen tool, unfortunately. I guess you could klick on the red icon with the X and the click event would be there?

No. I cant click on the red X. it must be an event on the grid?!

@Radzen Team: can you explain?

Regards
Thomas

Is a Button component in the DataGrid Column Template. You can use the template designer to reach that button:


With https://github.com/Basaingeal/Razor.SweetAlert2 can show a a message for confirm an action.

There is already simplified way to show dialogs including confirm:
https://blazor.radzen.com/dialog

Hello!

Doing this, every page with this dialog should then be excluded on Radzen Studio (not to be generated) ?

Thanks!

No, you can use the partial class of the page to define the method for showing simple dialog similar to the code in this thread:

Is there another overload for a function if someone selects the 'Yes' button?

Click=@(args => DialogService.Confirm("Are you sure?", "MyTitle", new ConfirmOptions() { OkButtonText = "Yes", CancelButtonText = "No" , YesButton=DoSomething() }))

this is fairly easy to do the steps are
1 - add your button
<RadzenButton Text="Delete confirmation" Click=@(args => DialogService.Confirm("Are you sure?", "All versions of this document will be deleted!", new ConfirmOptions() { OkButtonText = "Yes", CancelButtonText = "No" })) />

2 - In the OnInitialized subscribe

            DialogService.OnClose += CloseConfirmDelete;

3 - in the dispose unsubscribe

    public void Dispose()
    {
        // The DialogService is a singleton so it is advisable to unsubscribe.
        DialogService.OnClose -= CloseConfirmDelete;
    }

4 - add the function
void CloseConfirmDelete(dynamic result)
{
if(result != null) // if the user hits the x near the top right null is returned
{
// result is false if the user clicks no
if ((bool)result) DeleteDocument();
}
}

Warning - the function looks like it is getting called twice so you'll have to manage that as well.

1 Like

In this case, how do you know what document DeleteDocument has to delete? As by this point, you are already out of the context of the grid row.

1 Like

Hello @enchev,
I get the following Issue when calling the modal confirm Dialog with the Radzen version 3.17.2:
Is it a known issue? How can I fix it?
Thx in advance and kind regards
Gilles

System.NotSupportedException
HResult=0x80131515
Nachricht = Serialization and deserialization of 'System.Type' instances are not supported and should be avoided since they can lead to security issues. Path: $.ChildContent.Method.ReturnParameter.Member.DeclaringType.
Quelle = System.Text.Json
Stapelüberwachung:
at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex) in //src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs:line 472
at System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs:line 54 at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs:line 50 at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs:line 32 at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.String.cs:line 77 at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.String.cs:line 26 at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args) in /_/src/JSInterop/Microsoft.JSInterop/src/JSRuntime.cs:line 125 at Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__151.MoveNext() in /
/src/JSInterop/Microsoft.JSInterop/src/JSRuntime.cs:line 91
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in //src/libraries/System.Private.CoreLib/src/System/Runtime/ExceptionServices/ExceptionDispatchInfo.cs:line 56
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in /
/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/TaskAwaiter.cs:line 173
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in //src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/TaskAwaiter.cs:line 150
at System.Threading.Tasks.ValueTask1.get_Result() in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ValueTask.cs:line 789 at System.Runtime.CompilerServices.ValueTaskAwaiter1.GetResult() in /
/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ValueTaskAwaiter.cs:line 126
at Radzen.Blazor.RadzenDialog.d__6.MoveNext()

Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst:
System.Text.Json.Serialization.Converters.TypeConverter.Write(System.Text.Json.Utf8JsonWriter, System.Type, System.Text.Json.JsonSerializerOptions) in TypeConverter.cs
System.Text.Json.Serialization.JsonConverter.TryWrite(System.Text.Json.Utf8JsonWriter, T, System.Text.Json.JsonSerializerOptions, ref System.Text.Json.WriteStack) in JsonConverterOfT.cs
System.Text.Json.JsonPropertyInfo.GetMemberAndWriteJson(object, ref System.Text.Json.WriteStack, System.Text.Json.Utf8JsonWriter) in JsonPropertyInfoOfT.cs
System.Text.Json.Serialization.Converters.ObjectDefaultConverter.OnTryWrite(System.Text.Json.Utf8JsonWriter, T, System.Text.Json.JsonSerializerOptions, ref System.Text.Json.WriteStack) in ObjectDefaultConverter.cs
System.Text.Json.Serialization.JsonConverter.TryWrite(System.Text.Json.Utf8JsonWriter, T, System.Text.Json.JsonSerializerOptions, ref System.Text.Json.WriteStack) in JsonConverterOfT.cs
System.Text.Json.JsonPropertyInfo.GetMemberAndWriteJson(object, ref System.Text.Json.WriteStack, System.Text.Json.Utf8JsonWriter) in JsonPropertyInfoOfT.cs
System.Text.Json.Serialization.Converters.ObjectDefaultConverter.OnTryWrite(System.Text.Json.Utf8JsonWriter, T, System.Text.Json.JsonSerializerOptions, ref System.Text.Json.WriteStack) in ObjectDefaultConverter.cs
System.Text.Json.Serialization.JsonConverter.TryWrite(System.Text.Json.Utf8JsonWriter, T, System.Text.Json.JsonSerializerOptions, ref System.Text.Json.WriteStack) in JsonConverterOfT.cs
System.Text.Json.JsonPropertyInfo.GetMemberAndWriteJson(object, ref System.Text.Json.WriteStack, System.Text.Json.Utf8JsonWriter) in JsonPropertyInfoOfT.cs
System.Text.Json.Serialization.Converters.ObjectDefaultConverter.OnTryWrite(System.Text.Json.Utf8JsonWriter, T, System.Text.Json.JsonSerializerOptions, ref System.Text.Json.WriteStack) in ObjectDefaultConverter.cs
...
[Aufrufliste abgeschnitten]

Innere Ausnahme 1:
NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported and should be avoided since they can lead to security issues.

This exception is not related to Radzen Blazor dialog in any way.