Duplicates on postback in your ASP.net Dropdown Box
Let's say you are working on a dropdown box letting the user select a state and a county. You might want to have the county be filtered by the state, so you add an autopostback event to the state dropdown.
Let's also say, however, that you are allowing the search to proceed without selecting a county, so you add <asp:listitem Text="All Counties" Value="" /> as your first item and add appendDataBoundItems="True" to your counties dropdown.
Because you have now enabled autoappend, the act of changing the state will cause a postback and keep appending counties to your county dropdown box.
The solution? Add EnableViewState="False" to your Counties dropdown box. Yes, this may be obvious for some, but it's an easy thing to overlook and can lead you down a rather complex road of coding around viewstate that you may not want to explore.
Let's also say, however, that you are allowing the search to proceed without selecting a county, so you add <asp:listitem Text="All Counties" Value="" /> as your first item and add appendDataBoundItems="True" to your counties dropdown.
Because you have now enabled autoappend, the act of changing the state will cause a postback and keep appending counties to your county dropdown box.
The solution? Add EnableViewState="False" to your Counties dropdown box. Yes, this may be obvious for some, but it's an easy thing to overlook and can lead you down a rather complex road of coding around viewstate that you may not want to explore.
This comment has been removed by the author.
ReplyDelete