Add list items to dropdownlist in a formview with vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iamstuart
    New Member
    • Mar 2010
    • 8

    Add list items to dropdownlist in a formview with vb.net

    I have a dropdownlist in a formview that looks like this:
    Code:
    <asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True" 
    DataSourceID="SqlDataSource6" DataTextField="StaffName" 
    DataValueField="StaffName" SelectedValue='<%# Bind("LetterName") %>' 
    Width="155px">
    <asp:ListItem Value=""> </asp:ListItem>
    </asp:DropDownList>
    I am trying to add the value from a textbox which is also in the formview into the dropdownlist as a list item. I currently have this code for it:
    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
    This code works and adds the new item to the dropdownlist if I have no SelectedValue set for my dropdownlist. However when I set the selectedValue I get the error message:
    "'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 value does now exist in the list of items as it was added in from the textbox value. Does it try to do the selectedValue before adding my list item? If so how can I add the list item first, or do I need to set the selectedValue in the code after adding the list item?
  • aspdotnetuser
    New Member
    • Nov 2010
    • 22

    #2
    Just use this example to fit to your requirement

    Dim ddlValue As New ListItem

    ddlValue.Text = "Value Visible in Dropdown"
    ddlValue.Value = "Value on Selection"

    DropDownList1.I tems.Add(ddlVal ue)

    Comment

    • iamstuart
      New Member
      • Mar 2010
      • 8

      #3
      This code does not work any better, I still get the error:
      'DropDownList5' has a SelectedValue which is invalid because it does not exist in the list of items.
      Parameter name: value
      As it is still trying to get the SelectedValue before adding the list item

      Comment

      • iamstuart
        New Member
        • Mar 2010
        • 8

        #4
        I now have this working by removing the SelectedValue from the dropdownlist and setting the SelectedValue to the textbox.text in the VB code after adding the new value to the 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
                Dim ddlValue As New ListItem
        
                dropdown = FormView1.FindControl("dropdownlist5")
                listvalue = FormView1.FindControl("textbox1")
        
                ddlValue.Text = listvalue.Text
                ddlValue.Value = listvalue.Text
        
                dropdown.Items.Add(ddlValue)
        
                dropdown.SelectedValue = listvalue.Text
        
            End Sub

        Comment

        Working...