Is it possible to call c# Method from js?
Is it possible to call c# Method from js?
I want to recall C# while js catch error like below.
Util.InvokeScript(false, "eval", @$" try {{ // do something }} catch (error) {{ // call C# method }} ");
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
string UrlSweetAlert2Js = "https://cdn.jsdelivr.net/npm/sweetalert2@11"; Util.HtmlHead.AddScriptFromUri(UrlSweetAlert2Js); var js = """ Swal.fire({ title: 'Do you want to save the changes ? ', showDenyButton: true, showCancelButton: true, confirmButtonText: 'Save', denyButtonText: `Don't save` }).then((result) => { /* Read more about isConfirmed, isDenied below */ if (result.isConfirmed) { Swal.fire('Saved!', '', 'success'); //call C# Method 1 } else if (result.isDenied) { Swal.fire('Changes are not saved', '', 'info'); //call C# Method 2 } }); """; Util.InvokeScript(false, "eval", js);
-
- In C# create invisible button with
onClick
handler. - 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 }
- In C# create invisible button with
-
@i2van said:
1. In C# create invisible button withonClick
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 }
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...