调用 JS 警报或确认后的 Response.Redirect
我正在开发 VB.NET Web 应用程序。当有人成功更改密码时,我想显示一条弹出消息,让他们知道密码已成功更改。他们单击“确定”后,我想将他们重定向到主页。代码如下所示:
ClientScript.RegisterStartupScript(Me.GetType(), "confirmScript", "ConfirmNewUser();", True)
Response.Redirect("MainPage.aspx")
为什么会发生重定向并且警报弹出窗口从不显示?
I am working on a VB.NET web application. When someone successfully changes their password I want to show a popup message that lets them know it was changed successfully. After they click OK I want to redirect them to the main page. Code looks like this:
ClientScript.RegisterStartupScript(Me.GetType(), "confirmScript", "ConfirmNewUser();", True)
Response.Redirect("MainPage.aspx")
Why does the redirect happen and the alert popup never displays?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
1)从后面的代码中删除Response.Redirect。
2)更改ConfirmNewUser函数,如下所示:
Try this:
1) Remove Response.Redirect from the code behind.
2) Change the ConfirmNewUser function as given below:
您正在调用重定向服务器端,您的脚本永远没有机会运行。使用
window.location
进行客户端重定向,如下所示:You are calling the redirect server side, your script never get a chance to run. use
window.location
to do the redirect client side, something like this:原因是所有服务器端处理都会先于客户端进行。
一种解决方案是将“MainPage.aspx”传递给您的客户端脚本,如下所示:
然后您的客户端脚本必须采用 URL 参数:
并跟进 window.location:
并从服务器代码中删除以下内容:
The reason is because all server-side processing will take place prior to client-side.
One solution would be to pass "MainPage.aspx" to your client script as follows:
Your client script would then have to take a URL parameter:
and follow up with a window.location:
and remove the following from your server code:
Response.Redirect
设置 Location http 标头和 302-Moved 响应,浏览器一看到它就会对此采取行动。由于标题位于内容之前,因此您的脚本永远不会被看到或解析。Response.Redirect
sets the Location http header and a 302-Moved response, the browser will act upon this as soon as it sees it. As headers come before content, your script is never seen or parsed.