Setting SelectedValue for nested DropDownLists in DataList

C

Crazy Cat

HI,

I have a dropdownlist nested in a datalist.

In the datalist's ItemDataBound event I populate the dropdownlist and
set it's SelectedValue.
I am finding that instead of each DropDownList being set to
independent values all the dropdownlists' selectedvalue settings are
being set to the value of the last dropdownlist set. Here's the code
--

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").ToString

End If

End If

End Sub

Any idea what I could be doing wrong? I've set a breakpoint and can
see that different values are being set for the SelectedValue, but
when the page is displayed only the last value set is applied to the
SelectedValue for all the dropdownlists.

Thanks
 
B

bruce barker

because you add the same items to each list, they all behave the same.
setting a item to selected set it in all dropdowns becuase its the same
item. your code shoudl create new items for each dropdown.

-- bruce (sqlwork.com)
 
C

Crazy Cat

Made the change you suggest and it works like a charm. I guess I
didn't realize that under the covers setting SelectedValue actually
set the Selected value of the underlying listitem.

Thanks much Bruce.

I rewrote the code thusly --


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(New ListItem(item.Text,
item.Value))
Next

ddlImportance.SelectedValue = CType(e.Item.DataItem,
DataRowView).Row.Item("IncImp_ID").ToString

End If

End If

End Sub
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top