萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP.NET 程式設計

ASP.NET 後端呼叫前端 JavaScript 函式的正確姿勢

前言

在 Web Forms 中,後端 (C#) 需要觸發前端 JavaScript 函式執行,標準做法是使用 ClientScriptManager.RegisterStartupScript 或 ScriptManager.RegisterStartupScript (UpdatePanel 內)。

基本用法

// 一般 PostBack 頁面
ClientScript.RegisterStartupScript(
    this.GetType(),           // 唯一鍵型別
    "AlertKey",               // 唯一鍵字串
    "alert('伺服器端觸發的訊息!');", // JS 程式碼
    true                      // addScriptTags: 自動包 
						
copyright © 萬盛學電腦網 all rights reserved