Click here to Skip to main content
14,735,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a drop-down list, if I am selecting one value it is getting changed to another (only in some cases), what I have observed is DataValueField value is same for both but DataTextField value is different.
dtCurrency has two columns - shortcode and Long Name.
ddlCurrency.DataSource = dtCurrency;
        ddlCurrency.DataTextField = "shortcode";
        ddlCurrency.DataValueField = "Long Name";
        ddlCurrency.DataBind();
        ddlCurrency.Items.Insert(0, "Currency");


Example:
I m selecting TRY it is changing to TRL automatically,
same for BGN to BGL is changing.

dtCurrency:
shortcode	Long Name
BGL	Bulgarian Lev
BGN	Bulgarian Lev
TRL	Turkish Lira
TRY	Turkish Lira
INR     Indian Rupees
USD     US Dollors


What I have tried:

This is happening only when DataValueField is same. how to resolve this.
Posted
Updated 9-Apr-19 11:01am
Comments
CHill60 9-Apr-19 3:44am
   
Try putting "(Old)" at the end of the Long Name for BGL and TRL?
F-ES Sitecore 9-Apr-19 7:26am
   
You're using "Long Name" as the value so when you select BGL the value (ID) used is Bulgarian Lev, so if that is the ID how does it know you mean BGL and not BGN? IDs need to be unique, yours are not. You need to do something to make your ID (Long Name) unique. Are you sure you don't actually want shortcode as the value and Long Name as the text?

For Dropdownlist, there should be distinct values for the data value field. Still, the option can be used for the current situation is:

Give shortCode for both field, text and value and based on selected index data can be fetched out from the data table.
   
Quote:
This is happening only when DataValueField is same. how to resolve this.


That's a normal behavior of the DropDownList.

As others have mentioned, the DataValueField for your DropDownList must be unique so that the selection won't messed up. To fix that, use the shortcode field as the DataValueField. For example:

ddlCurrency.DataSource = dtCurrency;
ddlCurrency.DataTextField = "shortcode";
ddlCurrency.DataValueField = "shortcode";
ddlCurrency.DataBind();
ddlCurrency.Items.Insert(0, "Currency");


Now if you really want to display the Long Name field in the page then you can combine both fields and display it as one. That really depends on you. Again, the basic idea here is to make that your DataValueField values must be unique and doesn't have duplicate values.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900