ScriptManager.RegisterStartupScript 代码不起作用 - 为什么? [英] ScriptManager.RegisterStartupScript code not working - why?

查看:23
本文介绍了ScriptManager.RegisterStartupScript 代码不起作用 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去曾使用这样的代码在我的 asp.net 网页上成功弹出警报消息.现在它不工作了.我不明白为什么.

I have used code like this in the past to successfully pop up an alert message on my asp.net webpage. Now it is not working. I can't figure out why.

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

有什么想法吗?

推荐答案

我的头顶:

  • 使用 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);
}

这篇关于ScriptManager.RegisterStartupScript 代码不起作用 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆