If you just want to log the error, you could call external.log (see the sample 'Adding JavaScript - logging'). Or you could return a value to the caller when there's an exception.
@i2van said:
1. In C# create invisible button with onClick handler.
2. In JavaScript set button text to the data you would like to pass to C# and initiate button click.
string UrlSweetAlert2Js = "https://cdn.jsdelivr.net/npm/sweetalert2@11";
Util.HtmlHead.AddScriptFromUri(UrlSweetAlert2Js);
// Create button for receiving callback fron JavaScript.
var callbackButton = new LINQPad.Controls.Button ("", onClick: btn => btn.Text.Dump())
{
Visible = false
}.Dump();
var js = @$"
Swal.fire({{
title: 'Do you want to save the changes ? ',
showDenyButton: true,
showCancelButton: true,
confirmButtonText: 'Save',
denyButtonText: `Don't save`
}}).then((result) => {{
// Get callback button ID.
let b = document.getElementById('{callbackButton.HtmlElement.ID}');
if (result.isConfirmed) {{
Swal.fire('Saved!', '', 'success');
// Update button text.
b.textContent = '{nameof(Saved.Yes)}';
}} else if (result.isDenied) {{
Swal.fire('Changes are not saved', '', 'info');
// Update button text.
b.textContent = '{nameof(Saved.No)}';
}} else {{
// Update button text.
b.textContent = '{nameof(Saved.Cancel)}';
}}
// Do callback back to C#.
b.click();
}});
";
Util.InvokeScript(false, "eval", js);
enum Saved
{
Yes,
No,
Cancel
}
Comments
Maybe LIQPad is not a right tool for a job? Or HTML? For example, you can create an application using Avalonia, and use it in LINQPad.
Or you can return some error value from
Util.InvokeScript
and call C# code.If you just want to log the error, you could call external.log (see the sample 'Adding JavaScript - logging'). Or you could return a value to the caller when there's an exception.
I want to recall c# method by user's SweetAlert select dialog
onClick
handler.Thanks, It works perfectly
This is a PoC, so for production use I'd wrap implementation details in the class. Nothing like this can be found in LINQPad examples...