RegisterStartupScript没有为firefox asp.net编写任何脚本

时间:2015-08-12 11:54:32

标签: javascript asp.net visual-studio-2010 google-chrome firefox

这似乎是一个奇怪的场景,但asp.net的RegisterStartupScript不会在页面上为firefox抛出任何脚本。 这适用于铬。 代码如下:

ScriptManager.RegisterStartupScript(this, GetType(), "testfunc", "<script type='text/javascript'>function testfunc(){ alert('hi123'); } testfunc(); </script>", false);

当我这样做时,#34;查看页面来源&#34;对于chrome来说,代码是可见的,但对于Firefox来说它什么都没有。 此警告也会被激活,但很明显,对于firefox没有警报。

我还尝试使用上面提到的代码中的最后一个参数,即addScriptTags为真,并从内部删除脚本标记但无效。

以下是截图供您参考。 如果此信息很重要,则firefox版本为39.0,chrome版本为44.0.2。

enter image description here

如果我需要更多信息,请告诉我。

请帮忙!提前谢谢!

2 个答案:

答案 0 :(得分:0)

要向页面注册javascript,请使用ClientScriptManager.RegisterStartupScript。检查link是否正确使用

像这样使用

ClientScript.RegisterStartupScript(this.GetType(), "testfunc", "<script type='text/javascript'>function testfunc(){ alert('hi123'); } testfunc(); </script>", false);

或者像这样

this.ClientScript.RegisterStartupScript(this.GetType(), "alertscript","<script>alert('Hai');</script>");

答案 1 :(得分:0)

我通过将相关代码移动到页面的prerender事件来完成此工作。

所以代码如下所示

protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "testfunc", "<script type='text/javascript'>function testfunc(){ alert('hi123'); } testfunc(); </script>", false);
}

我找出问题here

的解决方案

希望这有助于其他人!

干杯!