繁体   English   中英

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

[英]ScriptManager.RegisterStartupScript code not working - why?

我过去曾使用这样的代码在我的asp.net网页上成功弹出警报消息。 现在它不起作用。 我无法弄清楚为什么。

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

有任何想法吗?

脱离我的头顶:

  • 使用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是您网页上Updatepanelid

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

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

我遇到了类似的问题。 然而,这个问题是由于我设计页面以引入请求的方式引起的。我将所有的.js文件放在最后应用于页面的内容中,因此它们位于我的文档的末尾。 .js文件包含我的所有功能。 脚本管理器似乎能够调用此函数它需要js文件已经存在与加载时调用的函数。 希望这有助于其他任何人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM