Javascript ScriptManager.RegisterStartupScript 代码不起作用 - 为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4994040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ScriptManager.RegisterStartupScript code not working - why?
提问by TheMoot
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.
我过去曾使用这样的代码在我的 asp.net 网页上成功弹出警报消息。现在它不工作了。我不明白为什么。
ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID,
"alert('This pops up')", true);
Any ideas?
有任何想法吗?
回答by Frédéric Hamidi
Off the top of my head:
在我的头顶:
- Use
GetType()
instead oftypeof(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:
- 使用
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);
}
回答by Eldhose
Try this code...
试试这个代码...
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "script", "alert('Hi');", true);
Where UpdatePanel1
is the id
for Updatepanel
on your page
您页面上UpdatePanel1
的id
forUpdatepanel
在哪里
回答by Abdelrahman Saeed
You must put the updatepanel id in the first argument if the control causing the script is inside the updatepanel else use the keyword 'this' instead of update panel here is the code
如果导致脚本的控件在 updatepanel 内,则必须将 updatepanel id 放在第一个参数中,否则使用关键字“this”而不是更新面板这里是代码
ScriptManager.RegisterStartupScript(UpdatePanel3, this.GetType(), UpdatePanel3.UniqueID, "showError();", true);
回答by NightDeveloper
I came across a similar issue. However this issue was caused because of the way i designed the pages to bring the requests in. I placed all of my .js files as the last thing to be applied to the page, therefore they are at the end of my document. The .js files have all my functions include. The script manager seems that to be able to call this function it needs the js file already present with the function being called at the time of load. Hope this helps anyone else.
我遇到了类似的问题。然而,这个问题是由于我设计页面来引入请求的方式造成的。我把我所有的 .js 文件作为最后应用到页面的东西,因此它们在我的文档的末尾。.js 文件包含我的所有功能。脚本管理器似乎能够调用这个函数,它需要在加载时调用函数的 js 文件已经存在。希望这对其他人有帮助。