Why is my dynamic bound dropdown losing its value?
Posted on 2008-10-24
I have a form that is created dynamically based on a widget type selected, e.g. Article Widget. The widget will then tell the code what controls need to be created in the grid, e.g. in this example its 6 dropdown lists, 3 for the categories and 3 for the pages - these are paired together.
Now I can bind it fine, and all the dropdowns appear correctly. All the category dropdowns have the correct details in, and the page dropdowns each have the first item set to "select a category". All good so far.
I select the first dropdown and the event that I added to the dynamically bound dropdown correctly fires e.g. ddlCategory.SelectedIndexChanged += new EventHandler(pageCategory_SelectedIndexChanged); and its paired dropdown correctly gets filled with the pages for that category. All other dropdowns at this point are still in their initial state. So all controls are being rebuilt on postback, and the viewstate for the category dropdown is being remebered. Again all good so far.
Now when I select a category in one of the other category dropdowns the SelectedIndexChanged event fires for both the first category dropdown and then the second dropdown. This again I believe is correct behaviour, so that the page dropdowns can then have the correct items in them to allow the viewstate to be put back correctly. However the first page dropdownlist loses its selected value - as if the viewstate is not being populated back.
Has anyone come across this before?