本文介绍了Dropdownlist选择的值会自动更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,如果我选择一个值,它将被更改为另一个值(仅在某些情况下),我观察到的是DataValueField值对于两者都是相同的,但DataTextField值是不同的。

dtCurrency有两列 - 短代码和长名称。

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");





例如:

我选择TRY它会自动更改为TRL,

相同的BGN到BGL正在改变。



dtCurrency:



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





我尝试过:



仅当DataValueField相同时才会发生这种情况。如何解决这个问题。



What I have tried:

This is happening only when DataValueField is same. how to resolve this.

推荐答案


Quote:

仅当DataValueField相同时才会发生这种情况。如何解决这个问题。

This is happening only when DataValueField is same. how to resolve this.





这是DropDownList的正常行为。



正如其他人提到的那样,你的 DropDownList DataValueField c> 必须是唯一的,以便选择不会搞砸。要解决此问题,请使用短代码字段作为 DataValueField 。例如:





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");





现在,如果你真的想显示长名在页面中的字段,然后您可以组合两个字段并将其显示为一个。这真的取决于你。同样,这里的基本思想是使你的 DataValueField 值必须是唯一的,并且没有重复的值。



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.


这篇关于Dropdownlist选择的值会自动更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 07:25