views:

53

answers:

3

I have a dropdownlist box, from which a user makes a selection. However, I am not able to retrieve the value of the SelectedItem in the code behind.

How can I get the value selected in the code behind?

        if (ddlRegion.SelectedValue = "0")
        {
            Response.Write("<script>window.alert('Please select a region')</script>");
            txtEmpID.Text = "";
            return;
        }
A: 

try SelectedValue property of dropdownlist instead of SelectedItem

sanjuro
I am using the SelectedValue, but no luck;
user279521
i dont know why, but for general options like Choose something, i use the value of "-1" not "0".
sanjuro
"-1" does not work either;
user279521
A: 

It looks like you're trying to compare to 0, are you trying to check if the dropdownlist is at its default state (which is the first value)? If so, SelectedIndex is the property you want, and you want to compare to the integer literal 0, not the string "0". Also, it's probably a copy/paste error as it doesn't compile as is, but you want to compare equality with ==, not make an assignment with =.

Tanzelax
A: 

(SelectedValue from MSDN) This property returns the Value property of the selected ListItem. The SelectedValue property is commonly used to determine the value of the selected item in the list control. If multiple items are selected, the value of the selected item with the lowest index is returned. If no item is selected, an empty string ("") is returned.

I'd go with the answer "Tanzelax" proposed, but none-the-less, you're comparing against "0" and the Microsoft documentation is telling you to compare against an empty string.

Isaac