簡體   English   中英

ASP.NET按鈕回發后的Javascript重定向

[英]Javascript Redirect after ASP.NET button postback

我想問一下在ASP.NET按鈕單擊事件完成后是否可以進行JavaScript重定向。

我之所以這樣問,是因為我正在編輯一個ASP.NET頁面,但是沒有訪問源代碼的權限,因此我只能更改事物的客戶端。

因此,總而言之,我希望能夠仍然運行ASP.NET按鈕事件,完成后,我需要重定向到另一個頁面。

我環顧四周並測試了一些,但是ASP.NET回發使客戶端中的重定向未運行。

任何想法?

<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>

2種選擇:

1)在服務器端提交表單時-執行以下操作:

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

2)使用隱藏字段並在Submit(在服務器端)設置值。 當頁面返回時,通過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'
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM