Link to home
Start Free TrialLog in
Avatar of SamCash
SamCashFlag for United States of America

asked on

ASP, Use duplicate values in DropDownList

EE,

I need to have some duplicate Values in my dropdownlist (for look up purposes).  I expected the control to operate on the Index, however it appears to be operating on Value instead???  If this is how dropdownlists work how do I accomplish this functionality??

Thank You
Sam

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ddlDupValues.aspx.cs" Inherits="WebApplication1.Debug.ddlDupValues" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>
        <br/>Selected Index:<asp:Label ID="lblIndex" runat="server" Text="zls"></asp:Label>
        <br/>Selected Value:<asp:Label ID="lblValue" runat="server" Text="zls"></asp:Label>
        <br/>Selected Text:<asp:Label ID="lblText" runat="server" Text="zls"></asp:Label>
    </div>
    </form>
</body>
</html>

Open in new window


using System;
using System.Web.UI.WebControls;

namespace WebApplication1.Debug
{
    public partial class ddlDupValues : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddl.Items.Insert(0, new ListItem("0_text", "0_value"));
                ddl.Items.Insert(1, new ListItem("1_text", "1_value"));
                ddl.Items.Insert(2, new ListItem("2_text", "2_value"));
                ddl.Items.Insert(3, new ListItem("3_text", "2_value"));
                ddl.Items.Insert(4, new ListItem("4_text", "3_value"));
                ddl.Items.Insert(5, new ListItem("5_text", "3_value"));
                ddl.Items.Insert(6, new ListItem("6_text", "3_value"));
                ddl.Items.Insert(7, new ListItem("7_text", "4_value"));
            }
        }
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblIndex.Text = ddl.SelectedIndex.ToString();
            lblValue.Text = ddl.SelectedValue;
            lblText.Text = ddl.SelectedItem.Text;
        }
    }
}

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

This might be a very silly question but how does a user know which is the 'correct' one to select when two or more options are identical to the user ?  (= ****BAD**** design)
First thing is to remember that ASP.net dropdown lists render <select> controls client side.
So knowing this, your select will be something like:
<select>
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
    <option value="3">Item 4</option>
    <option value="3">Item 5</option>
    <option value="2">Item 6</option>
    <option value="2">Item 7</option>
</select>

Open in new window

And <select> controls use the value attribute to... eh... set the value, it's not by index.
So in fact, you can have duplicate value sin the list, it doesn't matter, it will always take the value associated with the selected <option>.

So, what's your problem exactly?
From here, everything looks good.
Avatar of SamCash

ASKER

Andy,
Alexandre,

Thanks for the comments.

I showed the "Index" field in my sample to facilitate this discussion and highlight the problem.

Andy, I believe the webpage renders "Text" field not the "Value" Field, there are no duplicates in the "Text" field (the duplicates are in the "Value" field, so no confusion to the user.

Alexandre,  Yes that is what the code behind generates.  The problem is...

click ------- index ----- value ----- returns ---- index ------ value ------- selected text
0_text ---- 0 ------------0_value -- returns ----- 0 ------------ 0_value --- 0_text --- as expected
1_text ---- 1 ------------1_value -- returns ----- 1 ------------ 1_value --- 1_text --- as expected
2_text ---- 2 ------------2_value -- returns ----- 2 ------------ 2_value --- 2_text --- as expected
3_text ---- 3 ------------2_value -- returns ----- 2 ------------ 2_value --- 2_text --- error
4_text ---- 4 ------------3_value -- returns ----- 4 ------------ 3_value --- 4_text --- as expected
5_text ---- 5 ------------3_value -- returns ----- 4 ------------ 3_value --- 4_text --- error
6_text ---- 6 ------------3_value -- returns ----- 4 ------------ 3_value --- 4_text --- error
7_text ---- 7 ------------7_value -- returns ----- 7 ------------ 7_value --- 7_text --- as expected

row 3 error, the value is correct, but the index and text are wrong, s/b 3 and 3_text
row 5 error, the value is correct, but the index and text are wrong, s/b 5 and 5_text
row 6 error, the value is correct, but the index and text are wrong, s/b 6 and 6_text

I believe the only reason the values are correct is because the values are duplicate.  It appears the value is used as the key instead of the index.  It always sends the first value when there are duplicate values along with it's associated index and text, which is not correct.  You can see this behavior in the sample.  If you click 3_text it will return row 2 (index 2, value 2_value, and 2_text).

If you look at the EventArgs e you will see the code behind is getting the wrong index and text, so I can't see how the code behind could know which row was actually clicked.  I realize a work around is to make the value and text fields the same and then look up he correct values in a table. I am hoping there is a property I can set to make the dropdown use index as its key instead of the value field or something else, or see what I am missing.

A practical example, text and value field pairs where duplicate values would exist.
TV Dinners
Turkey, 3.59
Chicken, 3.59
Steak, 4.99

Timezones
CST, -6:00
Canada Central Time, -6:00
Central Standard Time(Mexico), -6:00

These all return the correct value to the code behind but when re-rendered the wrong text is selected.

Thanks again for your assistance on this issue.
Sam
re duplicates, OK that isn't so bad from a design point of view if the user is presented with distinct choices.  (I'm just puzzled what the duplicate - hidden - value with a unique text is?  It must make sense to you.)
Avatar of SamCash

ASKER

Andy,

Right now I have a dropdown list with timezonenames mined from a pc and mac (more to come).  The TimezoneNames make sense to the most users but the TimezoneOffset (-08:00 etc) may not.  Microsofts TimeZoneInfo enumerates many TimezoneNames for the same TimezoneOffset.  The Mac does the same.

I want the user to click on the familiar TimezoneName (Pacific Standard Time, PC or PST, Mac) and have the CORRECT (the one the user clicked) TimezoneName and TimezoneOffset returned to the codebehind.

I hope this helps.  I have wanted to do this elsewhere, but found a different ways to accomplish the functionally without using dropdowns.  I t would be alot easier if I could use the dropdown in this instance due to the differences in what the different os's return for TimezoneNames.

Thanks Again
Sam
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SamCash

ASKER

Andy,

Yes that would work, but I am trying not to make another round trip to IIS and SQL.  When the page is first loaded, I have a JavaScript function that gets the TimezoneName from the client (whether it be PC or Mac) with that TimezoneName JavaScript picks the selected item.  This gets them their local timezone as a default (they may select a different one)  they also pick other parameters before finally submitting.  The code behind processes this data with the selected TimezoneName and Offset,

Thanks for your assistance
Sam
There is no Canada Central Time or US Eastern Standard Time (saw this in another question) it is simply Central Standard Time (CST) and Eastern Standard Time (EST)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SamCash

ASKER

Thanks all,

Hope everyone had a great Thanksgiving.  Sorry for the "neglected" I got all last week off and where I went had no computers or cell phones!

So, I built a table with the first column as the TimezoneName from various sources (PC and MAC, David the two os,s return different results when the client queries either the list of TimezoneNames or the TimezoneName appended to Date();).  I am using this first column as the logical KEY, all unique,  and is the "Text" of the dropdown.  A second column is the TimezoneOffset, (-08:00:00) has duplicates, is the "Value" of the dropdown.  One example of usage, a small javascript function is fired on "<body onload>" and not a "postback".  The function gets the ClientTimezoneName from the client, parses the TimezoneName from var d = new Date().toString(); and selects BY TEXT,
ddl.options[i].text == ClientTimezoneName

Open in new window

, NOT BY VALUE.  This works on the 4 main browsers on MAC or PC.  This gives the desired result without hitting IIS or SQL a second time.

Thanks for all your assistance
Sam