爲了保持這種簡單,我在ASP.NET窗體中有一個下拉列表和一個按鈕。下拉列表具有調用DropDownList1_SelectedIndexChanged的autopostback函數,並且該頁面被重定向到某處(對於此示例,www.google.com),並且該按鈕具有一個onclick,該按鈕將轉到Button1_Click1,並且該頁面被重定向到www.yahoo.com。ASP.NET AutoPostBack然後後退按鈕奇怪發生
問題:如果我點擊按鈕,我會去雅虎,這是你所期望的。如果我在瀏覽器中點擊返回按鈕並選擇下拉列表,我會轉到Google,這也是正確的,但如果我點擊後退按鈕,然後點擊按鈕,我將重定向到Google。呃?爲什麼不去雅虎?
這裏是我的代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Testing Auto-Postback</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true" ValidationGroup="form1">
<asp:ListItem>Please select</asp:ListItem>
<asp:ListItem>Go to Google</asp:ListItem>
</asp:DropDownList>
<hr />
<asp:Button ID="Button1" runat="server" Text="Go to Yahoo"
ValidationGroup="form2" onclick="Button1_Click1" />
</form>
</body>
</html>
代碼背後:
using System;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("http://www.google.com");
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Redirect("http://www.yahoo.com");
}
}
如果有人可以幫助我,那將是十分讚賞。
謝謝你看這個。有什麼辦法可以阻止這種情況發生? – Jefferson
用可能的解決方法更新了答案。 –
再次感謝您。這些解決方案完美地工作,第二個是輝煌的,我喜歡簡單。非常感謝! – Jefferson