最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

event handling - In my Blazor Server side app: How can I fire an action when anywhere on the razor page's body is clicke

matteradmin7PV0评论

In my blazor server side app I want to listen to the mouseclick event on a razor page, and when mouseclick occurs (except on a button), I want to run a method. I have tried following with JavaScript but didn't worked for me:

in my _host.cshtml added following script:

window.addDocumentClickListener = () => {
  document.addEventListener('click', function (e) {
    if (e.target.tagName.toLowerCase() !== 'button') {
      DotNet.invokeMethodAsync('MyProjectName', 'HandleMouseClick');
    }
  });
};

in my Razor page:

@inject IJSRuntime JS
    ...
    ...
@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender )
        {
            await JS.InvokeVoidAsync("addDocumentClickListener");
        }

    }
    [JSInvokable]
    public void HandleMouseClick()
    {
        Action_On_Mouseclick();        
    }
}

In my blazor server side app I want to listen to the mouseclick event on a razor page, and when mouseclick occurs (except on a button), I want to run a method. I have tried following with JavaScript but didn't worked for me:

in my _host.cshtml added following script:

window.addDocumentClickListener = () => {
  document.addEventListener('click', function (e) {
    if (e.target.tagName.toLowerCase() !== 'button') {
      DotNet.invokeMethodAsync('MyProjectName', 'HandleMouseClick');
    }
  });
};

in my Razor page:

@inject IJSRuntime JS
    ...
    ...
@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender )
        {
            await JS.InvokeVoidAsync("addDocumentClickListener");
        }

    }
    [JSInvokable]
    public void HandleMouseClick()
    {
        Action_On_Mouseclick();        
    }
}
Share Improve this question edited Nov 25, 2024 at 9:29 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Nov 16, 2024 at 8:57 MdarendeMdarende 7951 gold badge12 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Here's a demo with no extra JS, using the Counter page. It has a full page div with click event handler. Note that you need to using stopPropagation="true" to prevent bubbling up on buttons and other UI elements with UI events.

@page "/counter"

<PageTitle>Counter</PageTitle>
<div class="underlay" @onclick="BackClick">
    <div>
        <h1>Counter</h1>

        <p role="status">Current count: @currentCount</p>

        <button class="btn btn-primary" @onclick:stopPropagation="true" @onclick="IncrementCount">Click me</button>
    </div>
</div>

<style>
    .underlay {
        position: fixed;
        width: 100%;
        height: 100%;
        background-color: transparent;
    }
</style>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        Console.WriteLine("Button Clicked");
        currentCount++;
    }

    private void BackClick()
    {
        Console.WriteLine("Background Clicked");
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far