I need to create a group of DropDownList
s to show and allow change of a property group for an item.
I have the following code on my ASP page.
<asp:Repeater runat="server" ID="repeaterProperties">
<ItemTemplate>
<p><asp:DropDownList runat="server" ID="ddProperty" OnInit="ddProperty_OnInit" /><p>
</ItemTemplate>
</asp:Repeater>
The ddProperty_OnInit
populates the DropDownList
with all the possible values with a database query.
How can I set the selected value of each created DropDownList
according to the Repeater
's source data?
Let's say, for example, that we have the possible property values of A
, B
and C
.
If the database output for the Repeater
contains two of those values, A
and B
, the Repeater
outputs two DropDownList
s, both with all 3 values availabla and the first one with A
as selected value and the second one with B
as selected value.
Edit:
It seems that adding OnItemDataBound="repeater_ItemDataBound"
to the Repeater
and selecting the appropriate value in there is not the way to go in my case. This is because I also need to save the possibly changed values to a database.
The ItemDataBound
event of the Repeater
is fired before the OnClick
event on a Button
and changes the selected values to their old values before the new selections can be saved.
Any suggestion on how to work around this?
Current code:
<asp:Repeater runat="server" ID="repeaterJako" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<asp:DropDownList id="ddJako" runat="server" OnInit="ddJako_OnInit">
</asp:DropDownList><br />
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" id="updateButton" Text="Save" OnClick="update_OnClick" />
In the code-behind, ddJako_OnInit
populates the drop down list with all the possible choises, while the repeater_ItemDataBound
uses the method suggested by Bryan Parker to select the proper value.