DropDownList SelectedValue lost between ItemDataBound and PreRender

C

Crazy Cat

Hi,

I am populating a dropdownlist in a DataList's ItemTemplate then
setting it's SelectedValue in the dropdownlist's ItemDataBound
property. However when the page is displayed the dropdownlist's
Selected Value is set to the largest value so that the bottom
selection appears selected. I actually printed out the SelectedValues
in the ItemDataBound and the PreRender events and can see that the
values change between the two events.

Here's the code --- (Importances is a collection of DataListItems)


Protected Sub dlIncentives_ItemDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles
dlIncentives.ItemDataBound

If Not IsPostBack Then

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType
= ListItemType.AlternatingItem Then
Dim ddlImportance As DropDownList =
e.Item.FindControl("ddlImportance")
For Each item As ListItem In Importances
ddlImportance.Items.Add(item)
Next

ddlImportance.SelectedValue = CType(e.Item.DataItem,
DataRowView).Row.Item("IncImp_ID")
System.Diagnostics.Debug.Print("ItemDataBound -
SelectedValue" & ddlImportance.SelectedValue)

End If

End If

End Sub

Protected Sub dlIncentives_PreRender(ByVal sender As Object, ByVal
e As System.EventArgs) Handles dlIncentives.PreRender
If Not IsPostBack Then
For Each item As DataListItem In dlIncentives.Items
Dim ddlImportance As DropDownList =
item.FindControl("ddlImportance")

System.Diagnostics.Debug.Print("PreRender -
SelectedValue" & ddlImportance.SelectedValue)

Next
End If
End Sub

My output window shows the following (the database has 4 rows of data)
-

ItemDataBound - SelectedValue 2
ItemDataBound - SelectedValue 5
ItemDataBound - SelectedValue 2
ItemDataBound - SelectedValue 6

PreRender - SelectedValue 6
PreRender - SelectedValue 6
PreRender - SelectedValue 6
PreRender - SelectedValue 6

Magically, between the ItemDataBound event and the PreRender event the
SelectedValue changes!!!!

What the F---!?!

Another MicroShaft "undocumented" "feature" I guess.

Can anyone help?

Thanks,
 
B

bruce barker

as you have the dropdown set to single selection, the last selection
wins. what did you expect?

-- bruce (sqlwork.com)
 
C

Crazy Cat

as you have the dropdown set to single selection, the last selection
wins. what did you expect?

I'm not setting the SelectedValue for the SAME dropdownlist control --
I am setting it for each dropdownlist control extracted by findcontrol
in the
current row accessed in the datalist.

Did you read the code I posted? Let me know if any of the variables
are unclear and I'll elaborate.

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,920
Messages
2,570,038
Members
46,449
Latest member
onedumbsquirrel

Latest Threads

Top