I have a dropdownlist in a formview which looks like this:
I am trying to add a list item into the dropdownlist to the value of a textbox that is also in the formview, to do this I am using this vb.net code:
Now, this works to add the textbox value to the dropdownlist if I do not set its selected value property, when I do set the selected value I get "'DropDownList5 ' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value "
This is the bit I dont understand because the selected value is in the list of items as it is the value I just added in from the textbox, is it checking for the selected value before adding the new list item? If so how do I add the item before the selected value or not set a selected value and have it set selected value in the VB code after adding items to the dropdownlist?
Code:
<asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource6" DataTextField="StaffName"
DataValueField="StaffName" SelectedValue='<%# Bind("LetterName") %>'>
<asp:ListItem Value=""> </asp:ListItem>
</asp:DropDownList>
Code:
Protected Sub Formview1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
Dim dropdown As DropDownList
Dim listvalue As TextBox
dropdown = FormView1.FindControl("dropdownlist5")
listvalue = FormView1.FindControl("textbox1")
dropdown.Items.Add(New ListItem(listvalue.Text, listvalue.Text))
End Sub
Parameter name: value "
This is the bit I dont understand because the selected value is in the list of items as it is the value I just added in from the textbox, is it checking for the selected value before adding the new list item? If so how do I add the item before the selected value or not set a selected value and have it set selected value in the VB code after adding items to the dropdownlist?