ASP.Net 浏览器历史记录中的下拉值不正确
ASP.Net Dropdown Value Not Correct in Browser History
我不敢相信我以前从来没有注意到这一点,但搜索并没有发现任何关于这个特别奇怪的东西。
在下面的代码中,如果您循环浏览下拉选项(select 依次为 2、3、4、5),您将在标签中看到正确的值。
现在,如果您单击浏览器的后退按钮,您将在下拉列表中看到 "Five",在标签中看到 "you selected item number Four"。
再次单击浏览器的后退按钮,您将在下拉列表中看到 "Four" 和 "you selected item number Three"
再次单击会出现相同的不匹配,您将在下拉列表中看到 "Three",在标签中看到 "you selected item number Two"。
什么给?我尝试设置一个 jquery 触发器来执行下拉菜单的回发,希望这会给浏览器时间 "register" 下拉菜单值发生变化的事实 - 但产生的结果与上面解释的相同
"code" 在这里:
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Test_DropdownHistory._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlHistoryTest" runat="server" OnSelectedIndexChanged="ddlHistoryTest_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="One" Value="One" />
<asp:ListItem Text="Two" Value="Two" />
<asp:ListItem Text="Three" Value="Three" />
<asp:ListItem Text="Four" Value="Four" />
<asp:ListItem Text="Five" Value="Five" />
</asp:DropDownList>
</div>
<div>
<asp:Label ID="lblHistoryTest" runat="server" Text="This is the default text" />
</div>
</form>
</body>
</html>
CS
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlHistoryTest_SelectedIndexChanged(object sender, EventArgs e)
{
lblHistoryTest.Text = "you selected item number " + ddlHistoryTest.SelectedValue;
}
}
我猜那天我的 GoogleFu 没电了。考虑之后,我意识到我应该关注事物的 JavaScript/HTML 方面。其中之一是我很快就在所有地方的 S/O 上找到了答案。我将其粘贴到全局脚本文件中并在路上:
$(document).ready(function () {
$("select").each(function () {
$(this).val($(this).find('option[selected]').val());
});
})
在此处找到:Select menu not being restored when Back button used
我不敢相信我以前从来没有注意到这一点,但搜索并没有发现任何关于这个特别奇怪的东西。
在下面的代码中,如果您循环浏览下拉选项(select 依次为 2、3、4、5),您将在标签中看到正确的值。
现在,如果您单击浏览器的后退按钮,您将在下拉列表中看到 "Five",在标签中看到 "you selected item number Four"。
再次单击浏览器的后退按钮,您将在下拉列表中看到 "Four" 和 "you selected item number Three"
再次单击会出现相同的不匹配,您将在下拉列表中看到 "Three",在标签中看到 "you selected item number Two"。
什么给?我尝试设置一个 jquery 触发器来执行下拉菜单的回发,希望这会给浏览器时间 "register" 下拉菜单值发生变化的事实 - 但产生的结果与上面解释的相同
"code" 在这里:
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Test_DropdownHistory._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlHistoryTest" runat="server" OnSelectedIndexChanged="ddlHistoryTest_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="One" Value="One" />
<asp:ListItem Text="Two" Value="Two" />
<asp:ListItem Text="Three" Value="Three" />
<asp:ListItem Text="Four" Value="Four" />
<asp:ListItem Text="Five" Value="Five" />
</asp:DropDownList>
</div>
<div>
<asp:Label ID="lblHistoryTest" runat="server" Text="This is the default text" />
</div>
</form>
</body>
</html>
CS
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlHistoryTest_SelectedIndexChanged(object sender, EventArgs e)
{
lblHistoryTest.Text = "you selected item number " + ddlHistoryTest.SelectedValue;
}
}
我猜那天我的 GoogleFu 没电了。考虑之后,我意识到我应该关注事物的 JavaScript/HTML 方面。其中之一是我很快就在所有地方的 S/O 上找到了答案。我将其粘贴到全局脚本文件中并在路上:
$(document).ready(function () {
$("select").each(function () {
$(this).val($(this).find('option[selected]').val());
});
})
在此处找到:Select menu not being restored when Back button used