I have a dropdownlist that triggers autopostback and fires the SelectedIndexChanged change event. Ive set viewstate to true but for some reason the selected value is not persisted between postbacks. Ive used the dropdownlist hundreds of times but cant seem to work out why this is happening. The items in the dropdownlist are declaratively coded e.g
<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>
Any ideas. Im stumped
<%@ 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>
heres the code behind
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);
}
}
The only reason I can think of, is you are setting some default value in your Page load event.. like..
Before the
SitePrefDropDownList_SelectedIndexChangedevent fires in the page life cycle, the Page_load event is called first and your Default/Old value will be resetEdit: Your page load should set the value like..