[英]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.