Okay, so I have a DropDownList control that contains 50 ListItems, one for each of the 50 states. The text value reflects each state name which makes each text value unique. However, the value property of each list item contains that state's current tax rate, which means that some of the values are not unique. Postbacks on this control constantly change the selected item. Is there a work around for this?
To duplicate, copy the code below into a new web form. It is just a simple DropDownList. Try selecting different states. Some of them stick, and others do not. For instance, try selecting "Idaho" as the state; it reverts to Arkansas for some reason. This is happening before the OnSelectedIndexChanged event even fires because if I select "Idaho" and then read the selected item in the event method on postback, the selected item is Arkansas! Very annoying.
State:<br />
<asp:DropDownList BorderWidth="3px" AutoPostBack="true" BackColor="#ffffcc" ID="ddlBillingState"
runat="server">
<asp:ListItem Value="0" Text="Not Specified"></asp:ListItem>
<asp:ListItem Value="4" Text="Alabama"></asp:ListItem>
<asp:ListItem Value="0" Text="Alaska"></asp:ListItem>
<asp:ListItem Value="5.6" Text="Arizona"></asp:ListItem>
<asp:ListItem Value="6" Text="Arkansas"></asp:ListItem>
<asp:ListItem Value="8.25" Text="California"></asp:ListItem>
<asp:ListItem Value="2.9" Text="Colorado"></asp:ListItem>
<asp:ListItem Value="6" Text="Connecticut"></asp:ListItem>
<asp:ListItem Value="6" Text="District of Columbia"></asp:ListItem>
<asp:ListItem Value="0" Text="Delaware"></asp:ListItem>
<asp:ListItem Value="6" Text="Florida"></asp:ListItem>
<asp:ListItem Value="4" Text="Georgia"></asp:ListItem>
<asp:ListItem Value="4" Text="Hawaii"></asp:ListItem>
<asp:ListItem Value="6" Text="Idaho"></asp:ListItem>
<asp:ListItem Value="6.25" Text="Illinois"></asp:ListItem>
<asp:ListItem Value="7" Text="Indiana"></asp:ListItem>
<asp:ListItem Value="6" Text="Iowa"></asp:ListItem>
<asp:ListItem Value="5.3" Text="Kansas"></asp:ListItem>
<asp:ListItem Value="6" Text="Kentucky"></asp:ListItem>
<asp:ListItem Value="4" Text="Louisiana"></asp:ListItem>
<asp:ListItem Value="5" Text="Maine"></asp:ListItem>
<asp:ListItem Value="6" Text="Maryland"></asp:ListItem>
<asp:ListItem Value="6.25" Text="Massachusetts"></asp:ListItem>
<asp:ListItem Value="6" Text="Michigan"></asp:ListItem>
<asp:ListItem Value="6.875" Text="Minnesota"></asp:ListItem>
<asp:ListItem Value="7" Text="Mississippi"></asp:ListItem>
<asp:ListItem Value="4.225" Text="Missouri"></asp:ListItem>
<asp:ListItem Value="0" Text="Montana"></asp:ListItem>
<asp:ListItem Value="5.5" Text="Nebraska"></asp:ListItem>
<asp:ListItem Value="6.85" Text="Nevada"></asp:ListItem>
<asp:ListItem Value="0" Text="New Hampshire"></asp:ListItem>
<asp:ListItem Value="7" Text="New Jersey"></asp:ListItem>
<asp:ListItem Value="5" Text="New Mexico"></asp:ListItem>
<asp:ListItem Value="4" Text="New York"></asp:ListItem>
<asp:ListItem Value="5.75" Text="North Carolina"></asp:ListItem>
<asp:ListItem Value="5" Text="North Dakota"></asp:ListItem>
<asp:ListItem Value="5.5" Text="Ohio"></asp:ListItem>
<asp:ListItem Value="4.5" Text="Oklahoma"></asp:ListItem>
<asp:ListItem Value="0" Text="Oregon"></asp:ListItem>
<asp:ListItem Value="6" Text="Pennsylvania"></asp:ListItem>
<asp:ListItem Value="7" Text="Rhode Island"></asp:ListItem>
<asp:ListItem Value="6" Text="South Carolina"></asp:ListItem>
<asp:ListItem Value="4" Text="South Dakota"></asp:ListItem>
<asp:ListItem Value="7" Text="Tennessee"></asp:ListItem>
<asp:ListItem Value="6.25" Text="Texas"></asp:ListItem>
<asp:ListItem Value="4.7" Text="Utah"></asp:ListItem>
<asp:ListItem Value="6" Text="Vermont"></asp:ListItem>
<asp:ListItem Value="5.0" Text="Virginia"></asp:ListItem>
<asp:ListItem Value="6.5" Text="Washington"></asp:ListItem>
<asp:ListItem Value="6" Text="West Virginia"></asp:ListItem>
<asp:ListItem Value="5" Text="Wisconsin"></asp:ListItem>
<asp:ListItem Value="4" Text="Wyoming"></asp:ListItem>
</asp:DropDownList>
I even tried disabling viewstate, then OnSelectedIndexChanged I would store the selected index in ViewState["selectedIndex"] and retrieve it on postback and repopulate the drop down list. But as I said before, because the selected item is a different value than the one intended before it even gets to the event, it was just storing the screwy value in my custom viewstate field.
I think your values should be unique. Instead of putting the tax rate as the value, instead let the value simply be the state, and then use that state to obtain your tax value. As one example, in your code-behind, you could have a Dictionary<string, decimal> taxTable
that you use to get your values. Such as
decimal taxRate = taxTable[yourDropDown.SelectedValue];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With