且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET 用户控件中的 Javascript 函数

更新时间:2023-12-06 09:58:34

您需要使用 ClientScriptManager - 这样它们就可以注册一次,无论控件添加到页面的频率如何:

You need to register your scripts with ClientScriptManager - this way they can be registered once, regardless of how often the control has been added to the page:

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
  String cstext1 = "alert('Hello World');";
  cs.RegisterStartupScript(cstype, csname1, cstext1, true);
}