我有一个下拉列表,它会触发自动回传并触发SelectedIndexChanged更改事件。我已将ViewState设置为true,但由于某种原因,在回传之间选定的值未被保留。我已经使用过下拉列表数百次,但似乎无法弄清楚为什么会发生这种情况。下拉列表中的项是通过声明性编码实现的,例如:
<asp:DropDownList ID="SitePrefDropDownList" runat="server" AutoPostBack="True"
onselectedindexchanged="SitePrefDropDownList_SelectedIndexChanged" EnableViewState="true">
<asp:ListItem Value="Proffesional">Proffesional</asp:ListItem>
<asp:ListItem Value="Colorful">Colorful</asp:ListItem>
</asp:DropDownList>
任何想法。我被难住了。
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Proffesional.master" AutoEventWireup="true" EnableViewState="true"
CodeFile="Home.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="Label1" runat="server" Text="User Name: "></asp:Label>
<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Color Scheme: ">
</asp:Label><asp:DropDownList ID="SitePrefDropDownList" runat="server"
AutoPostBack="True"
onselectedindexchanged="SitePrefDropDownList_SelectedIndexChanged" EnableViewState="true">
<asp:ListItem Value="Proffesional">Proffesional</asp:ListItem>
<asp:ListItem Value="Colorful">Colorful</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Button ID="OKButton" runat="server" Text="OK" onclick="OKButton_Click" />
</asp:Content>
这是代码的后台。
public partial class _Default : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e) {
if (Session["Template"] != null) {
string MasterPage = String.Format("~/{0}.master", (string)Session["Template"]);
MasterPageFile = MasterPage;
}
}
protected void Page_Load(object sender, EventArgs e){
}
protected void OKButton_Click(object sender, EventArgs e) {
if (UserNameTextBox.Text.Length != 0) {
Session["UserName"] = UserNameTextBox.Text;
Label Welcome = (Label)Master.FindControl("GreetingLabel");
Welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);
}
}
protected void SitePrefDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
Session["Template"] = SitePrefDropDownList.SelectedValue;
Server.Transfer(Request.Path);
}
}
DataBind
(例如缺少IsPostBack
检查)? - Tim SchmelterServer.Transfer(Request.Path);
? - Graham Clark