Disabling viewstate on drop down list causes problems
I have a page that has two drop down lists on it and a button. All controls sit in an update panel. The first drop down list (ddl1) contains a lot of values which caused a huge viewstate so I disabled viewstate on it. In the page oninit event I populate the drop down list every time the page is posted back. This all seems to work fine, it shows all the values and I can access the selected value in my event handler.
ddl1 has the autopostback attribute set to true so when its value changes the second drop down list (ddl2) is populated with some values that depend on the selection in ddl1. This also works fine, the values in ddl2 change when I select a value in ddl1.
ddl2 does not have viewstate disabled. The button that sits under my two drop down lists is disabled by default and becomes enabled when the value of ddl2 is changed to anything except string.empty. This is where I run into problems. ddl2 does an autopostback too but it seems to lose the selected index/value. When I'm in my code to check whether to enable or disable the button the selected index is always 0 and the selected val开发者_JAVA百科ue is string.Empty.
If I enable viewstate on ddl1 this all works fine. Could anyone point me to what I'm doing wrong?
Here is the code executed by the ddl2 postback:
protected void AvailableProgramsIndexChanged(object sender, EventArgs e)
{
ToggleMoreInformationButton();
}
private void ToggleMoreInformationButton()
{
if (Request.Browser.Type.Contains("IE"))
{
ToggleIE();
}
else
{
ToggleNonIE();
}
}
private void ToggleIE()
{
if (this.ddlAvailablePrograms.SelectedValue != string.Empty)
{
this.careerInfoLearnMoreSubmit.Enabled = true;
this.careerInfoLearnMoreSubmit.CssClass = "submit nongreyed";
}
else
{
this.careerInfoLearnMoreSubmit.Enabled = false;
this.careerInfoLearnMoreSubmit.CssClass = "submit greyed";
}
}
private void ToggleNonIE()
{
if (this.ddlAvailablePrograms.SelectedValue != string.Empty)
{
this.careerInfoLearnMoreSubmit.Enabled = true;
this.careerInfoLearnMoreSubmit.Style.Remove("opacity");
this.careerInfoLearnMoreSubmit.Style.Add("opacity", "1.0;");
}
else
{
this.careerInfoLearnMoreSubmit.Enabled = false;
this.careerInfoLearnMoreSubmit.Style.Remove("opacity");
this.careerInfoLearnMoreSubmit.Style.Add("opacity", "0.5;");
}
}
The code does not modify the selected value of ddl2 at all.
Thanks,
b3nI'm guessing the code running during the postback is clearing out ddl2 because the selected value is not coming back (because ViewState is off). But without some sample code, it's really hard to say for sure.
精彩评论