I need to store an item and its corresponding price into an array and then display these.
E.g.
book at 10.00
chocolate bar at 0.85
I will populate the item and price into a dropdown. On a button click event I want to add these selections into an array.
How would I go about doing this in vb.net?
<asp:DropDownList ID="ddlItem" runat="server">
<asp:ListItem Value="12.49">book</asp:ListItem>
<asp:ListItem Value="14.99">music</asp:ListItem>
<asp:ListItem Value="0.85">chocolate bar</asp:ListItem>
<asp:ListItem Value="10.00">box of chocolates 1</asp:ListItem>
<asp:ListItem Value="47.50">bottle of perfume 1</asp:ListItem>
<asp:ListItem Value="27.99">bottle of perfume 2</asp:ListItem>
<asp:ListItem Value="18.99">bottle of perfume</asp:ListItem>
<asp:ListItem Value="9.75">headache pills</asp:ListItem>
<asp:ListItem Value="11.25">box of chocolates 2</asp:ListItem>
</asp:DropDownList>
The code in the button click event seems to always overwrite what I initially added.
Protected Sub btnAdd_Click(sender As Object, e As System.EventArgs) Handles btnAdd.Click
'Add items into an array list
Dim Item As String = ddlItem.SelectedItem.Text
Dim Price As Decimal = ddlItem.SelectedValue
Dim ListItemCollection As ListItemCollection = New ListItemCollection
ListItemCollection.Add(Item)
Response.Write(ListItemCollection.Count)
End Sub
On the server side / code behind, you should be able to access your DropDownList by name;
ddlItem.The
ddlItem.Itemsmember exposes a ListItemCollection, which in turn implements IEnumerable.IEnumerable has a ToArray extension method. Therefore, it should be as simple as
ddlItem.Items.ToArray().Hope that helps.
EDIT
Having read your comment, I understand I may have misread your question. The answer is still pretty simple though, code as shown: