![](/img/trans.png)
[英]Possible to run a javascript function after postback using an asp.net button?
[英]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.