473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

I have a dropdownlist in a formview that looks like this:
Expand|Select|Wrap|Line Numbers
  1. <asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True" 
  2. DataSourceID="SqlDataSource6" DataTextField="StaffName" 
  3. DataValueField="StaffName" SelectedValue='<%# Bind("LetterName") %>' 
  4. Width="155px">
  5. <asp:ListItem Value=""> </asp:ListItem>
  6. </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:
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub Formview1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
  2.  
  3.         Dim dropdown As DropDownList
  4.         Dim listvalue As TextBox
  5.         dropdown = FormView1.FindControl("dropdownlist5")
  6.         listvalue = FormView1.FindControl("textbox1")
  7.  
  8.         dropdown.Items.Add(New ListItem(listvalue.Text, listvalue.Text))
  9.     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?
Nov 26 '10 #1

✓ answered by iamstuart

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.

Expand|Select|Wrap|Line Numbers
  1.     Protected Sub Formview1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
  2.  
  3.         Dim dropdown As DropDownList
  4.         Dim listvalue As TextBox
  5.         Dim ddlValue As New ListItem
  6.  
  7.         dropdown = FormView1.FindControl("dropdownlist5")
  8.         listvalue = FormView1.FindControl("textbox1")
  9.  
  10.         ddlValue.Text = listvalue.Text
  11.         ddlValue.Value = listvalue.Text
  12.  
  13.         dropdown.Items.Add(ddlValue)
  14.  
  15.         dropdown.SelectedValue = listvalue.Text
  16.  
  17.     End Sub

3 2329
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.Items.Add(ddlValue)
Nov 27 '10 #2
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
Dec 1 '10 #3
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.

Expand|Select|Wrap|Line Numbers
  1.     Protected Sub Formview1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
  2.  
  3.         Dim dropdown As DropDownList
  4.         Dim listvalue As TextBox
  5.         Dim ddlValue As New ListItem
  6.  
  7.         dropdown = FormView1.FindControl("dropdownlist5")
  8.         listvalue = FormView1.FindControl("textbox1")
  9.  
  10.         ddlValue.Text = listvalue.Text
  11.         ddlValue.Value = listvalue.Text
  12.  
  13.         dropdown.Items.Add(ddlValue)
  14.  
  15.         dropdown.SelectedValue = listvalue.Text
  16.  
  17.     End Sub
Dec 1 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Peter Collinson | last post by:
Hi... Is there any way to style a List Item a different color and size than the <LI> in an Ordered List? I'd like a red super-script number and a dark blue text in a page's footnotes. And...
2
by: Unknown User | last post by:
How can I separate list items displayed inline by vertical bars | ? If I use a background image that simulate a vertical bar, and I put this bg image behind each list item, the last list item will...
2
by: deepakp | last post by:
Hi, I'm trying to create a site index page. The page layout will consist of a header, left navigation and content display. The left navigation and content display should look like the image file...
1
by: RSH | last post by:
Hi, I have a situation where I need to add several "Hidden" properties to list items in a dropdownlist. By default the DropDownList item has two properties with regards to the listitems...
0
by: John Dalberg | last post by:
using VS 2005. I have an unordered list with list items which are displayed horizontally. Some of the list items have hyper links and some have checkboxes. The problem is that the...
5
pezholio
by: pezholio | last post by:
Hi, I've got an inline list with a label and checkbox in each list item, each item comes from a MySQL database and, chances are that, in future, the list will become longer. My problem is that...
2
by: Max58kl | last post by:
Hi I am trying to create a bulletted list that has a colored background. The follow css code successfully applies the background color to the list items, but for some reason does not apply the...
4
by: Keith Hughitt | last post by:
For example, If you have a list: <ul> <li>item 1 is short.</li> <li>item 2 is a little bit longer</li> </ul> regardless of the size of the contents of each list item, the element
0
by: c0mrade | last post by:
I have a problem with getting the list items, below is my hibernate code, after that code there is my method ..and below that is my junit test. How can I make sure that query is executing properly,...
16
Frinavale
by: Frinavale | last post by:
What I'd like to do is take a list and display it's items in a circle. For example (the order of the in the circle doesn't matter..except for Item1, it has to be at the top): Item1 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.