What is the difference between
DropDownList.ClearSelection();
and
DropDownList.SelectedIndex = -1;
while working with a dropdownlist?
Edit : I am aware of the definitions for these available at MSDN. Can someone provide differences in implementation/practical use.
Looking into the source for System.Web.UI.WebControls.ListControl
, from which DropdownList
is derived, it seems like setting SelectedIndex
actually calls ClearSelection()
; and if not -1, it will proceed to select the item.
public virtual void ClearSelection() {
for (int i=0; i < Items.Count; i++)
Items[i].Selected = false;
}
public virtual int SelectedIndex {
set {
...
if ((Items.Count != 0 && value < Items.Count) || value == -1) {
ClearSelection();
if (value >= 0) {
Items[value].Selected = true;
}
}
...
}
Edit: so in answer to your question, calling ClearSelection()
directly would save you from a couple of (inconsequential) if statements..
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