简体   繁体   中英

Use the selected value of Dropdown list (value from database)

I want to use the value of the selected item from a Dropdown list to evaluate a condition. Please note that the value of DropTraining is from the database and consist of alphanumeric characters (ex: TRN-2015-2354)

Here is my initial code that I tried:

string valueTrain = DropTraining.SelectedValue; //put the value of droplist to valueTrain

    if (valueTrain == null)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(),
    "alert",
    "alert('The Training value is empty. Select value.');",
    true);
    }
    else
    { //code to execute if the valueTrain is not empty/null

It runs, but even I meant to select nothing from the DropTraining, it still executes the else statement. Can you please suggest a way/method for me to use the said value, or anything that may solve this, cheers

You can use it, selected value MUST be integer.

(ex: TRN-2015-2354) this is not integer.

string selected = drp.SelectedItem.Text;

Or

string selected = drp.SelectedItem.Value;

AND

int x = int.Parse(selected.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