Reputation: 176
I have a page that lists about 10-50 DDL. They are all dynamically created and filled. There are different sets of these DDL. At the top of the page there is a DDL and submit button that updates all of the DDL with the newly selected set.
Depending on user interaction some of the DDL's can have pre-set selected values. These are pulled from SQL and are applied. This works fine the first time a DDL set is selected. But when I select a new DDL set the selected values are incorrect even though the value it's given is correct (see picture).
Code:
Note: Each ddl is given a different ID (not shown)
If displayArray.Count = 1 Then
ddl.Enabled = False
ddl.Items.Add(New ListItem("Please use manuel entry", -1))
Else
ddl.Enabled = True
' Add default selection
ddl.Items.Add(New ListItem("Not Assigned", -1))
For Each item In displayArray
ddl.Items.Add(New ListItem(displayArray(count), valueArray(count)))
count += 1
Next
End If
If preSelReviewer <> -1 Then
ddl.SelectedValue = preSelReviewer
Else
ddl.SelectedValue = -1
End If
Generally what happens is if I select a set in which the first DDL is disabled and then change to a set with no disabled DDL's the first DDL ddl.SelectedValue
isn't applied (even though when I step through the code it is.
I know it has something to do with the ddl.Enabled
because the moment I remove that everything works fine.
Edit
This seems to happen anytime I change something on a specific DDL. Such as:
ddl.CssClass
or ddl.Visible
.
Upvotes: 0
Views: 3056
Reputation: 176
The viewstate (because it comes after pageload) was overriding my controls. Thus setting the pages EnableViewState="false"
fixed the problem.
Upvotes: 0
Reputation: 11
The first thing I would check is that the value you are trying to set to your DDL is indeed in your dataset. DDL's cannot be assigned values that do not exist within their set; Which is opposite behavior of the "Drop Down" variant of a combobox. (From what I gather this isn't actually the issue)
Next you want to make sure there isn't an event that is overwriting you DDL.SelectedValue.
If the first two things check out I would try using DDL.Text, only because i've found that to be more consistent in my trials for setting a ComboBox. This may not be best practice, but I haven't run into any issues with .Text where I have seen some weird things with setting SelectedValue. With that being said if I was populating the variable I would use SelectedValue over Text.
I hope I haven't missed the point completely here.
Upvotes: 1