我已經使用這段代碼成功地從 c# 呼叫了一個 javascript 函式,但我似乎只能在每個 c# 方法中使用它一次。如何在一個方法中呼叫多個函式?我對 C# 有點陌生。
protected void btnAdd_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "firstEvent(event)", true);
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "secondEvent(event)", true);
}
uj5u.com熱心網友回復:
在使用 Blazor Webassembly 時,我遇到了類似的問題。我使用的解決方案是等待每個電話。
如果腳本管理器不允許直接等待它們,請嘗試:
protected async void btnAdd_Click(object sender, EventArgs e)
{
await Task.Run(() => ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "firstEvent(event)", true));
await Task.Run(() => ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "secondEvent(event)", true));
}
這可能是因為腳本管理器不能同時支持多個呼叫,所以它可能會忽略第二個呼叫。至少,這就是發生在我身上的事情。
uj5u.com熱心網友回復:
好的,所以你的問題是兩個方面。
請記住,該命令所做的只是將一些腳本添加到網頁,然后將其發送到客戶端。
因此,有兩種方法可以做到這一點。簡單地將兩個函式呼叫合二為一,如下所示:
ScriptManager.RegisterStartupScript(This.Page, Page.GetType,
"text", "test1();test2();", True)
你也可以這樣做:
ScriptManager.RegisterStartupScript(This.Page, Page.GetType, "text", "test1();", True)
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, "text2", "test2();", True)
注意我們如何給一個新的“鍵”名稱——如果你不這樣做,那么第二個會覆寫下一個。
但是,即使這樣也行不通:
ScriptManager.RegisterStartupScript(this.Page, Page.GetType, "text", "test1()", True)
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, "text2", "test2()", True)
注意缺少的“;” 在函式之后
所以
Test1() - only works for one
Test1(); - will work if you add more
所以,如果你去掉尾隨的“;”,那么腳本將被注入為
Test1()Test2()
你需要
Test1();Test2();
因此,要么將兩個呼叫都放在一個腳本注入中,例如
Test1();Test2();
或者用你的兩個腳本注入呼叫?
然后你需要一個不同的鍵名,并且還必須添加尾隨“;” 因此它們被視為單獨的 js 函式呼叫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411169.html
標籤:
