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

The*_*oot 19 javascript asp.net scriptmanager

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

ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, 
     "alert('This pops up')", true);
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Fré*_*idi 41

脱离我的头顶:

  • 使用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);
}
Run Code Online (Sandbox Code Playgroud)

  • 错过了; .添加它并且它有效.谢谢! (4认同)
  • @vNext,你是什么意思?[RegisterStartupScript()]的第一个参数(http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerstartupscript.aspx)是一个`Control`或`Page`,取决于在使用的重载,永远不是一个字符串. (3认同)

小智 19

试试这个代码......

ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "script", "alert('Hi');", true);
Run Code Online (Sandbox Code Playgroud)

您页面上UpdatePanel1idfor Updatepanel在哪里