简体   繁体   English

在 Blazor web 页面中使用 @onclick 调用多个函数

[英]Call multiple functions using @onclick in Blazor web pages

Two functions in blazor component should be called when I click on the button.当我单击按钮时,应该调用 blazor 组件中的两个函数。 Tried with comma, semicolon and parenthesis.尝试使用逗号、分号和括号。 Nothing works.没有任何效果。

  <div class="col-md-auto"><button type="button" class="btn btn-light btn-sm" @onclick="@SearchTable"@*call two methods SearchTable and SortTable*@>Search</button></div>

The two functions are given below下面给出这两个函数

@functions{
    public void SearchTable()
    {
        foreach (KeyValuePair<int, SearchCriteria> entry in SearchCritRefs)
        {
            entry.Value.setSearchCriteria(entry.Key);
        }
    }

    public void SortTable()
    {
        foreach (KeyValuePair<int, SortCriteria> entry in SortCritRefs)
        {
            entry.Value.setSortCriteria(entry.Key);
        }
    }

}

Do something like this:做这样的事情:

<div class="col-md-auto"><button type="button" class="btn btn-light btn-sm"
    @onclick="@(() => { SearchTable(); SortTable(); })">Search</button></div>

<div>@message1</div>
<div>@message2</div>

@code{
    private string message1;
     private string message2;

   
    public void SearchTable()
    {
       message1 = "SearchTable";
    }

    public void SortTable()
    {
       message2 = "SortTable";
    }

}

Note: I've shown you how to call two methods when the click event is triggered as this is your question.注意:我已经向您展示了如何在触发点击事件时调用两种方法,因为这是您的问题。 If it is wise to code like that is something altogether different.如果这样编码是明智的,那就完全不同了。

I would adopt the approach of calling a single function which can then call as many subsequent functions as required.我会采用调用单个 function 的方法,然后可以根据需要调用尽可能多的后续函数。

<button type="button" class="btn btn-light btn-sm" @onclick="@PerformSearch">Search</button>
@code{
    
    public void PerformSearch()
    {
        SearchTable();
        SortTable();
    }

    public void SearchTable()
    {
        foreach (KeyValuePair<int, SearchCriteria> entry in SearchCritRefs)
        {
            entry.Value.setSearchCriteria(entry.Key);
        }
    }

    public void SortTable()
    {
        foreach (KeyValuePair<int, SortCriteria> entry in SortCritRefs)
        {
            entry.Value.setSortCriteria(entry.Key);
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个项目中的 Blazor 页面 - Blazor pages in multiple projects 是否可以在多个母版页中调用Web表单? - Is it possible to call a web form in multiple master pages? 无法在 Blazor 中调用 Web API - Unable call Web API in Blazor JSRuntime.InvokeVoidAsync 在 Blazor 服务器应用程序调用多个函数时我只告诉它调用一个 - JSRuntime.InvokeVoidAsync in Blazor Server app calling multiple functions when I only tell it to call one 如何使用静态渲染模式调用 blazor onclick 方法 - How to call blazor onclick method with static render mode 无法使用 Blazor 服务器应用程序调用安全下游 Web API 使用 Azure AD B2C - Cannot get a Blazor Server App to call a secure downstream Web API using Azure AD B2C 运行 razor 托管页面,blazor web 程序集,IIS 上的应用程序 10 - Running razor pages hosted, blazor web assembly, application on IIS 10 使用Web Api服务进行多次呼叫 - Multiple call using Web Api Service 如何使用 Selenium Chrome 驱动程序在 chrome 中打开多个 web 页面 - How to open multiple web pages in chrome using Selenium Chrome Driver 使用c#一次访问多个网页 - accessing multiple web pages at a time using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM