I have a Gridview in which i have two templatefields of dropdownlist. I bound them on runtime with same list item.
li = new listitem ('1','1'); dl1.items.add(li); dl2.items.add(li); li = new listitem ('2','2'); dl1.items.add(li); dl2.items.add(li); li = new listitem ('3','3'); dl1.items.add(li); dl2.items.add(li); dl1.selectedvalue = '2'; dl2.selectedvalue = '3';
After executing above, dl1 & dl2 both show me ‘3’ as selected value. Why?
I know the work around of using 2 different listitems while binding but i wanna know why the above happens?
The ListItem class has a property ‘Selected’ which marks if the item is selected. I haven’t checked the DDL SelectedValue property to see what it does, but my guess is that the ListItem.Selected property is being set to true, and since you are using the same object in both drop-down lists, it is being marked as ‘selected’ in both.
I’m sure if this was a multi-select list, both ‘2’ and ‘3’ would be marked as ‘selected’.