Should the selectedindex or selectedvalue be used to set a asp.net dropdownlist?
I am wondering if there is a preferred or proper way to set the item in the dropdownlist in asp.net?
I am binding months to a list 0-13 items default selection is 0 months duration, and years 0-8 items and 0 years is the default selection.
As I know there are always going to be 12 months in a year, I could use selectedvalue or selectedindex without any perceived problem.
Is there a reason why I should not use either/or of the two properties to select a list item?
I hope this que开发者_运维技巧stion is clear enough.
I would suggest always using SelectedValue.... It's more predictable that way.
Imagine if you were told to add some new items, e.g "Please select" at index 0 and forgot to update your code!
I would use the SelectedValue when binding to an object within a DataSource. If you're populating the list manually with some numbers, then I see no reason to not use SelectedIndex.
UPDATE
It's early, and I wasn't thinking straight. Always use SelectedValue
as SelectedIndex is not predictable. Plus you know what the value is, so it's just easier.
精彩评论