views:

1877

answers:

5

I'm handling the onSelectIndexChanged event. An event is raised when the DropDownList selection changes. the problem is that the DropDownList still returns the old values for SelectedValue and SelectedIndex. What am I doing wrong?

Here is the DropDownList definition from the aspx file:

<div style="margin: 0px; padding: 0px 1em 0px 0px;">
    <span style="margin: 0px; padding: 0px; vertical-align: top;">Route:</span>
    <asp:DropDownList id="Select1" runat="server" onselectedindexchanged="index_changed" AutoPostBack="true">
    </asp:DropDownList>
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>

Here is the DropDownList OnSelectedIndexChanged event handler:

protected void index_changed(object sender, EventArgs e)
{
    decimal d = Convert.ToDecimal( Select1.SelectedValue );
    Literal1.Text = d.ToString();
}
+2  A: 

Do you have any code in page load that is by chance re-defaulting the value to the first value?

When the page reloads do you see the new value?

Mitchel Sellers
yeah, that's the first thing I thought of as well.
Danimal
I checked and found to my surprise that I did have some code in Page_Load that is re-defaulting the value. Thank you Mitchel!
minty
I had the same problem once.
axk
A: 

Is it possible that you have items copied throughout your datasource for the drop down list?

Donn Felker
+2  A: 

add this: if page.isnotpostback {

} around your code to bind the dropdownlist.

+1  A: 

This may seem obvious, but anyway. Do you initialize this dropdown with an initial value in some other event handler like OnLoad ? If so you should check if that event is risen by a postback or by the first load. So you should have something like

if(!IsPostback) d.SelectedValue = "Default"
axk
+3  A: 

If you are using AJAX you may also be doing a callback, not a full postback. In that case you may want to use this in your page load method:

        if (!IsCallback && !IsPostBack)
        {
            // Do your page setup here
        }
Jason Stevenson