views:

22

answers:

1

On an aspx page I have a dropdownlist and a button.

I select a value from the dropdownlist and click the button.

In the button event handler I have the statment: DropDownList1.SelectedValue;

This value is showing up as the first item in the list regardless of what item is actually selected.

Same result with .SelectedItem.Value and .Text

Why? And how do I get the item actually selected?

+1  A: 

Ok, i create an answer from my comment.

Do you rebind your Dropdown on every postback to the Datasource or do you have the same value for every item?

Always check for Page.IsPostback when you bind controls to a Datasource.

Tim Schmelter
the button is actually a remove button. So the list has say two items. I selected the second and click the remove button. The event handler is supposed to get the selected value and remove it and then rebind. However I can't do that because i'm getting the first item in the list not the selected one.
kralco626
You didn't even have answered one of my two questions that could cause a wrong selected item. Btw, what is theSelectedIndex of the Dropdown? Please show us some code of your databinding and event handler.
Tim Schmelter
Sorry. I was researching the other two answers. didn't want to speculate. I was binding in the pageload which is unnecessary because i do the bind both when the add button is clicked and when the remove button is clicked. I removed these binds from my page load altogether and it works. Thanks! your mention of the binding on postback lead me to the answer.
kralco626