ScriptManager.RegisterStartupScript代码不工作 – 为什么?

过去我使用过这样的代码来成功地在我的asp.net网页上popup警告消息。 现在它不工作。 我无法弄清楚为什么。

ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "alert('This pops up')", true); 

有任何想法吗?

closures我的头顶上:

  • 使用GetType()而不是typeof(Page)为了将脚本绑定到实际的页面类而不是基类,
  • 传递一个键常量而不是Page.UniqueID ,这是没有意义的,因为它应该由命名控件使用,
  • 用分号结束你的Javascript语句,
  • PreRender阶段注册脚本:

 protected void Page_PreRender(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", "alert('This pops up');", true); } 

试试这个代码…

 ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "script", "alert('Hi');", true); 

UpdatePanel1是您的页面上的UpdatePanel1id

如果引发脚本的控件位于updatepanel内部,则必须将updatepanel ID放在第一个参数中,否则使用关键字“this”而不是更新面板,这里是代码

 ScriptManager.RegisterStartupScript(UpdatePanel3, this.GetType(), UpdatePanel3.UniqueID, "showError();", true); 

我遇到了类似的问题。 然而,这个问题是由于我devise页面来引入请求的方式引起的。我将所有的.js文件作为最后一个应用到页面的东西,因此它们在我的文档的末尾。 .js文件包含我所有的function。 脚本pipe理器似乎能够调用这个函数,它需要已经存在的js文件,并在加载时调用该函数。 希望这可以帮助其他人。