简体   繁体   English

Page.ClientScript.RegisterStartupScript 不起作用 - 为什么?

[英]Page.ClientScript.RegisterStartupScript doesn't work - why?

i have some code in OnInit hanlder我在 OnInit 处理程序中有一些代码

if (!Page.ClientScript.IsStartupScriptRegistered(GetType(), "MyScript"))
{
    Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", GetStartupScript(), true);
}

here i try to register some java script code.在这里我尝试注册一些java脚本代码。 and i want it to work on button click event.我希望它可以处理按钮单击事件。 but it doesn't execute.但它不执行。 it executes only after refreshing page.它仅在刷新页面后执行。 can anyone explain me why it doesn't execute?谁能解释我为什么它不执行?

thnx in advance!提前谢谢!

Try this:试试这个:

ScriptManager.RegisterStartupScript(this, typeof(string), "Error", 
    "alert('hi');", true);

The issue is that on some pages you might have declared a ScriptManager .问题是在某些页面上您可能已经声明了ScriptManager Only one ScriptManager is allowed per page so you have to use the existing ScriptManager to register any scripts.每页只允许一个ScriptManager因此您必须使用现有的ScriptManager来注册任何脚本。

Note that the RegisterStartupScript is a static method;请注意, RegisterStartupScript是一个静态方法; do not call it on the instance of your ScriptManager (it will cause a compile error in C# but only a warning in VB.NET).不要在ScriptManager的实例上调用它(它会在 C# 中导致编译错误,但在 VB.NET 中只会导致警告)。

This link has a bit more info on this issue. 这个链接有更多关于这个问题的信息。

 string msg = "This is variable message";
 Page.ClientScript.RegisterStartupScript(typeof(Page), "well1", "<script>alert('" + msg + "');</script>");

这对我来说很好用:

Response.Write("<script type='text/javascript'>alert('" + AlerteMsg + "');</script>");

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

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