简体   繁体   English

无法从下拉列表中获取价值

[英]Unable to get value from drop-down list

I have a problem when i try to recover selected value from drop-down list. 我尝试从下拉列表中恢复选定的值时遇到问题。 If i select 2nd or 3rd drop-down list value code returns first value of drop-down list. 如果我选择第二或第三下拉列表值代码,则返回下拉列表的第一个值。 My code: 我的代码:

 drprdepart.SelectedItem.ToString();

采用

drprdepart.SelectedItem.Value.ToString();

Below is the working code used in my project, 以下是我的项目中使用的工作代码,

It will add the details to the Dropdown control: 它将详细信息添加到Dropdown控件中:

DD_CurrencyCode.DataSource = ds.Tables[0];
DD_CurrencyCode.DataTextField = "name";
DD_CurrencyCode.DataValueField = "active_currency_code";
DD_CurrencyCode.DataBind();

It will fetch code from the dropdown: 它将从下拉列表中获取代码:

string ddCurrencyCode = DD_CurrencyCode.Text; 字符串ddCurrencyCode = DD_CurrencyCode.Text;

Hope the code was useful,please let me know if its works for you. 希望代码有用,请让我知道它是否对您有用。

Try 尝试

drprdepart.SelectedItem.Value.ToString();

Or 要么

drprdepart.SelectedValue.ToString();

drprdepart.SelectedItem.Value for get the string drprdepart.SelectedItem.Value用于获取字符串

if you ind objects into dropdown 如果将对象放入下拉菜单

(Object Type)drprdepart.SelectedItem will convert selected item into selected object (对象类型)drprdepart.SelectedItem会将所选项目转换为所选对象

Are you binding your Dropdown list inside IsPostBack ? 您是否在IsPostBack内绑定了下拉列表? if not that's why its always getting 1st value selected 如果不是,那就是为什么总是选择第一个值

 if (!IsPostBack)
      {
         // Bind dropdown here
       }

and the get the selected value and text using 然后使用获取所选的值和文本

    drprdepart.SelectedItem.Value.ToString();
    drprdepart.SelectedItem.Text.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM