Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Is it possible to mix Javascript and C# code in a DOM event?

$
0
0

I'm trying to achieve the next in Blazor WebAssembly; I need to be able to listen for an event in a component, and use both Javscript and C# code like the next way:

<button onclick="console.log('Showing event from Js: '+ event); @Test()">Test</button>@code {   public void Test()   {       JSRuntime.InvokeVoidAsync("console.log", "Showing log from C#");   }}

The first problem is that I get the error "cannot implicitly convert type void to object", so if I change the Test method signature just to return an object

public object Test(){    JSRuntime.InvokeVoidAsync("console.log", "Showing log from C#");    return null;}

The App compiles but once the page is loaded, the "Test()" function is executed automatically, and if I click the button, it just executes the Javascript code, and not both pieces of code.

I know I should handle events in Blazor prefixing the event name with "@" symbol in order to call a C# method, and execute Javascript code there using the interop, but in that way, I can't use the default Javascript "event" which I need to use, instead of the Blazor "event" version.

Thanks in advance!


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>