ASP.NET - Tip - DataBinding DropDownLists

ASP.NET Databinding DropDownList Bug

Synopsis:

The Viewstate will not work correctly if a dropdownlist value field is databound to a string field.

The Issue:

I make no claim to being a superior coder but when I do make mistakes it would be nice to find some documentation leading to what I might have done wrong. In this case I foolishly bound the value field of a dropdownlist control in asp.net to a string property in a collection class I created. I did this really without even thinking. I didn't have an appropriate alternative field to bind to the value property of the dropdownlist but I did have a string field and strangely enough asp.net doesn't balk when you give it one. Shortly after I tried to run my web application I noticed the dropdownlist was not remembering what items were selected in the dropdownlist upon postback. After what seems like hours of trying to determine the cause I did away with databinding the value field and magically the dropdownlist resumed normal functioning. I also noticed that if the value field is not unique the viewstate goes nuts and the selected item doesn't quite work as expected. So in conlclusion remember to only use the value function as it's intended which is as a numeric identifier for the dropdown item, otherwise the item selection isn't retained on postback for item selection.

About this page: