I'm trying to refresh page when an item of the drop down list is selected.
DropDownList 永远不会得到最后选择的项目,它总是恢复保存在 ViewState 中的项目。ViewState 不想记住新的东西,它总是保留一个相同的信息。
默认的 ASPX 页面
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ExperimentalPage.aspx.cs"
Inherits="ExperimentalPage" Title="Enperimental Page" EnableViewState="True" %>
<asp:Content ID="ExperimentalLeftPane" ContentPlaceHolderID="cph_LeftPane" Runat="Server">
<div style="position: absolute; left: 0px; top: 35px; width: 250px;">
<asp:DropDownList ID="dd1" runat="server" OnSelectedIndexChanged="RefreshPage" AutoPostBack="True" CssClass="class_DropDown" Style="width: 250px; margin-bottom: 3px;"/>
</div>
</asp:Content>
默认 ASPX.CS 代码隐藏页面
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dd1.Items.Insert(0, "dd1-0");
dd1.Items[0].Value = "0";
dd1.Items.Insert(1, "dd1-1");
dd1.Items[1].Value = "1";
dd1.Items.Insert(2, "dd1-2");
dd1.Items[2].Value = "2";
}
else
{
dd1.SelectedIndex = dd1.Items.IndexOf(dd1.Items.FindByValue(Convert.ToString(Request.QueryString["dd1"])));
}
}
protected void RefreshPage(object o, EventArgs e)
{
Page.Response.Redirect("ExperimentalPage.aspx?"
+ "&dd1=" + dd1.SelectedValue);
}
问题部分解决
看我的回答