Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist with duplicate values

Status
Not open for further replies.

JasonNevin

Programmer
May 26, 2005
29
GB
Good morning

I've just found out that the value field in a DropDownList must be unique. In which case does anybody know of a way of dealing with duplicate values? I want to be able to select from a long list which automatically assigns my selection a score.

i.e Red=0,Blue=1,Green=0,Yellow=0,Orange=1.

If you code this using a normal dropdownlist and select Green the value Red is actually selected because the value is used to select the item. Anyone got a workaround?
 
Why don't you just set the value to the actual text and do your validation based on the text? e.g.
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not Page.IsPostBack Then
            Dim li As ListItem

            li = New ListItem
            li.Text = "Red"
            li.Value = "Red"
            DropDownList1.Items.Add(li)

            li = New ListItem
            li.Text = "Green"
            li.Value = "Green"
            DropDownList1.Items.Add(li)

            li = New ListItem
            li.Text = "Blue"
            li.Value = "Blue"
            DropDownList1.Items.Add(li)
        End If

    End Sub

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

        Select Case DropDownList1.SelectedItem.Value

            Case "Red", "Green"
                ' Do something

            Case "Blue"
                ' Do something else

        End Select
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry, I should have explained a bit more. I'm readling the values from a database table so hard-coding anything is not really an option.
 
How about just appending the value onto the end of the text then. e.g

Text = Red Value = Red_0
Text = Green Value = Green_0
Text = Blue Value = Blue_1


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Why don't you have a column in your table called value or score that holds the value you want to assign to that color? Then assign that column to the drop down's value column.

Jim
 
jbenson001

I already have that column. It contains the duplicate values I've spoken about. I could use it to lookup after the postback but it means two DB reads. ca8msm's method only requires one DB read.

Thanks.
 
Ahh sorry, I missed that you had already gotten those values...

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top