views:

105

answers:

1

Hi,

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 value 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,
b3n

A: 

I'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.

Jeff Siver
Hi Jeff, I added some code above.
b3n
From the sample above, is ddlAvailablePrograms ddl1 or ddl2 in your problem description? And, do you have any code in page_load as that will also execute on the postback?
Jeff Siver
ddl2 is available programs. I did not include the code for ddl1 as it works fine. The page_onload and oninit look like this:
b3n
protected override void OnInit(EventArgs e) { base.OnInit(e); InitDropDownLists(); }
b3n
protected override void OnLoad(EventArgs e) { if (!IsPostBack) { ddlCareers.Attributes["onChange"] = string.Format("javascript:SetLoadingMessage('{0}', '{1}');", ddlCareers.ClientID, ddlAvailablePrograms.ClientID); careerInfoLearnMoreSubmit.Attributes["onClick"] = string.Format("javascript:PerformRedirect('{0}');", ddlAvailablePrograms.ClientID); ToggleMoreInformationButton(); } }
b3n
I noticed that when the page is posted back from ddl2 the postbackhandler of ddl1 runs too. I think this could cause the issue as this handler resets the selection in ddl2 when populating it again with the same values. The only question is why would that handler run again? Does .Net trigger the autopostback method of ddl1 when it applies the previously selected value?
b3n
I agree that the postback handler for ddl1 running again is causing the problem. And, I believe that because ViewState is disabled on ddl1, the event processor is assuming the selected value changed triggering the AutoPostBack method. To fix it, I think you will have to add some code to the DDL1 postback to determine if the selected value was changed or not. You could do this through a Hidden Field (used to contain the selected value) or session.
Jeff Siver
I will give this a go, thanks.
b3n