且构网

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

ScriptManager.RegisterStartupScript 代码不起作用 - 为什么?

更新时间:2023-12-05 22:59:04

我的头顶:

  • 使用 GetType() 而不是 typeof(Page) 以便将脚本绑定到您的实际页面类而不是基类,
  • 传递一个键常量而不是 Page.UniqueID,这没有那么有意义,因为它应该被命名控件使用,
  • 以分号结束您的 Javascript 语句,
  • PreRender阶段注册脚本:
  • Use GetType() instead of typeof(Page) in order to bind the script to your actual page class instead of the base class,
  • Pass a key constant instead of Page.UniqueID, which is not that meaningful since it's supposed to be used by named controls,
  • End your Javascript statement with a semicolon,
  • Register the script during the PreRender phase:
protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", 
        "alert('This pops up');", true);
}