javascript 从 ASP.net 调用 ScriptManager.RegisterStartupScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25027949/
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
calling ScriptManager.RegisterStartupScript from ASP.net
提问by Sara N
I have a problem with calling a javascript Function from ASP.net; all the samples I've seen are in Button click Event, but I want to check something in Page_load and if the result was FALSE , show the user an alert and Redirect to another page. I tried these codes , but none of them runed, and the page redirected without any Alert
我在从 ASP.net 调用 javascript 函数时遇到问题;我看到的所有示例都在按钮单击事件中,但我想检查 Page_load 中的某些内容,如果结果为 FALSE ,则向用户显示警报并重定向到另一个页面。我尝试了这些代码,但它们都没有运行,并且页面在没有任何警报的情况下重定向
1.
1.
protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey",
"alert('my message');", true);
}
2.
2.
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert","alert('my message');", true);
Response.Redirect("~/register.aspx", false);
return;
}
I Also tried "ScriptManager.RegisterClientScriptBlock", but it wasnt worked. thanks in advanced
我也试过“ScriptManager.RegisterClientScriptBlock”,但没有奏效。先谢谢了
回答by Hirav Sampat
if you want to check something on page load with javascript then use
如果您想使用 javascript 检查页面加载时的某些内容,请使用
$(document).ready(function () {
//Your code here
} );
or in javascript
或在 javascript 中
window.onload
回答by Leo
Your second code snippet will not work because you are redirecting the response before the page gets sent to the browser. Your first code snippet should work, however, you need to add the redirection logic right after the call to alert
using client-side script. You can set the window.location
property with the url you want to redirect the user to
您的第二个代码段将不起作用,因为您正在将页面发送到浏览器之前重定向响应。您的第一个代码片段应该可以工作,但是,您需要在调用alert
using 客户端脚本后立即添加重定向逻辑。您可以window.location
使用要将用户重定向到的 url设置该属性
protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey",
"alert('my message');window.location='YOUR_URL_HERE';", true);
}
回答by Sean T
You say you have tried RegisterClientScriptBlock
with no joy. I use it in a page load event to set a bunch of user permissions on my page with no issue. Do you have a script manager present in your front end?
你说你尝试过RegisterClientScriptBlock
并没有快乐。我在页面加载事件中使用它在我的页面上设置一堆用户权限,没有问题。你的前端有脚本管理器吗?
<asp:ScriptManager ID="ScriptManager1" runat="server" />
It should be place directly after your opening <form>
tag, then server side in your page load call it like this...
它应该直接放在你的开始<form>
标签之后,然后你的页面加载中的服务器端像这样调用它......
ClientScript.RegisterClientScriptBlock(this.GetType(), "callJs", "yourJsfunction();", true);