views:

553

answers:

5

I have a weird problem with a dropdownbox selectedIndex always being set to 0 upon postback. I'm not accidentally rebinding it in my code. In fact I've placed a breakpoint at the very first line of the page_load event and the value is already set to zero. The dropdown is in the master page of my project, I don't know if that makes a difference. I'm not referencing the control in my content holder.

If I set my autoPostBack = 'true' the page works fine. I don't have to change any code and the selectedIndex is maintained. I have also tried setting enableViewState on and off and it doesn't make a difference. At this point I'm grasping at straws to figure out what's going on. I've never had this problem before.

Here is the code in my page_load event.

        If CartEstablished Then
            txtCustNum.Visible = False
            btnCustSearch.Visible = False
            lblCustNum.Visible = True
            ddlSalesType.Visible = False
            lblSalesType.Visible = True
            ddlTerms.Visible = False
            lblTerms.Visible = True

            lblTerms.Text = TermsDescription
        Else
            txtCustNum.Visible = True
            btnCustSearch.Visible = True
            lblCustNum.Visible = False

            lblSalesType.Visible = False
            ddlSalesType.Visible = True
            lblTerms.Visible = False
            ddlTerms.Visible = True
        End If

        If Page.IsPostBack Then
             GetUIValues()
        Else

             LoadTermCodes()
        End If

The LoadTermCodes is where I bind the dropdownlist that is causing me problems.

+1  A: 

Are you sure you are doing a postback and not a refresh? It is hard to help you without more context into the problem or a chunk of the code.

Gilligan
Ditto: WHen this happens to me, it is either 1) lack of Page.IsPostBack check or 2) Me refreshing rather than using a PostBack
bentford
A: 

This may be barking up the wrong tree, but a couple of things that have bitten me in the past that left me scratching my head:

  • Naming the input element a duplicated/reserved word (thinking "name", "method", "reset", etc.)
  • having the form element physically outside of the form being submitted

I find that when all the logical debugging turns up nothing, my own dumbness has created time-wasting "mystery" bugs like this on occasion.

I'm quite sure it's something stupid i'm doing.
Haydar
the name of the control that is causing problems is ddlTerms
Haydar
A: 

At what stage in the page lifecycle are you binding the dropdownlist? If you're binding in page_init it should work, if you're binding in page_load make sure you wrap a !IsPostBack around the binding commands.

If you post the code in question it'd be easier to troubleshoot.

Derek Lawless
I added the code that i use in the page_load event
Haydar
A: 

This may simply be a syntax error, but shouldn't

 If Page.IsPostBack Then
             GetUIValues()
        Else

Look like this

  If  NOT Page.IsPostBack Then
         GetUIValues()
    Else
Gilligan
you can do it eather way. there are things i want to do when the page is posted back and other's i want to do if the page isn't posted back.
Haydar
Then I want to say that the issue is probably in one of those two functions, but is it hard to say without knowing what they do.
Gilligan
A: 

I'm finding the same problem... in my case, the dropdownlist is filled by a javascript function after another dropdownlist onchange client event. On PageLoad, the 2nd dropdownlist has lost all its items and so its selectedIndex turns to 0. Is there any way of preventing this?