简体   繁体   中英

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:

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;

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

if you ind objects into dropdown

(Object Type)drprdepart.SelectedItem will convert selected item into selected object

Are you binding your Dropdown list inside 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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