views:

2419

answers:

4

Hi Guys,

I've got this asp.net drop down control that displays other textbox controls when the value is not UK (this is to help find UK addresses using postcodes). When UK is reselected I will like to hide the other controls. I've enabled view state and AutoPostBack to true. I have an onSelectedIndexChanged event that only gets fired once (when the dropdown value changes to a different country as by default it's UK)

I'll like to have the OnSelectedIndexChanged to fire every time the value is different but this isn't the case. Please help.

Cheers

p.s. Here's the code snippet, the site doesn't allow the server side controls to be displayed, I hope it's clear? thanks

"...DropDownList runat="server" ID="Country2" AutoPostBack="True" OnSelectedIndexChanged="Country2_SelectedIndexChanged" DataSource="<%# RegionList %>" DataTextField="Name" DataValueField="Code" CssClass="dropdown country">...

protected void Country2_SelectedIndexChanged(object sender, EventArgs e) { DropDownList d = (DropDownList)sender; addressEntry.CountryPrePostBack_SelectedIndexChanged(d.SelectedItem.Value); }

A: 

How are you attaching the event? Are you using doing it code behind like:

this.dropDownList.SelectedIndexChanged += new EventHandler(dropDownList_SelectedIndexChanged);

Or are you assigning it in the ASPX/ ASCX?

If it's the former make sure that you are not assigning it within a !IsPostback condition, you need to ensure that the event it added every postback.

Have you tried using the debugger to see whether a postback is actually occuring in subsiquent events? It could be that you're newly added controls are causing a validation failure, if you are using validators make sure you set the drop down list to CauseValidation = false so it'll postback every time.

Slace
Thanks for the reply Slace. I am assigning it in a user control *.ascx
A: 

I can only think of two questions to ask.

1.) Are you populating (binding) to the list on the page_load event? if so then you need to only do this the first time the page loads. When you bind to the control (or other controls) it will reset the selected index. If viewstate is enabled then it will keep the original list. 2.) Now I'm going ot assume that the above is already true so that would lead me to ask if viewstate is enable fro the parent of this page. If you turn trace on and look at the page control list you should be able to see the viewstate size for this particular item. If it's got a value then you know you are setting viewstate correctly. If not then work your way up the parent controls to see where viewstate ends.

Viewstate is necessary to detect a postback so it's important to see that it's working properly.

Now I’m a VB.net programmer and I noticed that in your sub there doesn’t appear to be a handler there. In vb.net we would normally see something like

Private Sub LinkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton.Click

The only other way tot get the event to fire properly is to include it in the button but you seem to be doing that correctly already. You might want to try it in the manner that I’ve put above and see if that changes anything for you.

Andrew

Middletone
+1  A: 

I've seen similar behavior when a javascript error has been introduced upon the first postback.

I think I saw this when the first postback caused a new div to be displayed (using javascript, not code-behind), and the div wasn't in the HTML. So the "show(div)" javascript referred to a missing object.

Granted, a very specific case, but I'd recommend checking for any js errors after the first postback.

Michael

kaneuniversal
+1  A: 

Every time I've ever had something like this happen it's been because of validation controls. Do those additional textboxes have validators attached to them that could be suppressing the postback? A quick way to tell would be to set CausesValidation="false" on the dropdownlist.

zakster82