ASP.NET按钮回发后的Javascript重定向

时间:2013-12-19 09:44:13

标签: javascript asp.net

我想问一下,在ASP.NET按钮点击事件结束后我们是否可以进行javascript重定向。

我之所以这样说是因为我正在编辑一个ASP.NET页面,但却无法访问源代码,因此我只能更改客户端的内容。

总而言之,我希望能够仍然运行ASP.NET按钮事件,在完成后我需要重定向到另一个页面。

我环顾四周并测试了一些但是ASP.NET回发使得客户端的重定向没有运行。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

<head runat="server">
<script type="text/javascript">
   function PageRedirect() {
       window.location.href = "http://www.google.com/";
   }
 </script>
</head>
<body>
<form runat="server">

//使用OnClientClick

     <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="PageRedirect(); return false;"/>
// Return False will not post the page even if its postback=true
</form>
</body>

答案 1 :(得分:0)

2个选项:

1)在表单提交时在服务器端 - 执行此操作:

Page.ClientScript.RegisterStartupScript(GetType(), "blabla", "window.location='xxx'", true);

2)使用隐藏字段并在提交时设置值(在服务器端)。当页面返回时,通过JS检查其值并执行以下操作:

if (document.getElementById('myHidden').value=='afterPostBack')
{
 window.location='xxx'
}

修改

在表单提交之前 - 在JS中执行此操作

var form = document.getElementById("idOfForm");
form.onsubmit = function() {
  document.cookie="isRedirect=1; expires=Thu, 28 Nov 2013 02:23:26 GMT; path=/";
  this.submit();

}

并在页面底部添加此代码:

if (document.cookie.indexOf('isRedirect=1')>-1)
{
 window.location='xxx'
}
相关问题