I am having a problem to change a selected item in a drop-down.
The way I use is (a property in the code behind which sets the new selection):
public char Candy
{
set
{
var newSelection = ddlCandy.Items.FindByValue(value.ToString());
ddlCandy.ClearSelection();
newSelection.Selected = true;
}
}
Is this a recommended and proper way?
Safe way is fist Find the given item from DropDownList and set it as SelectedValue
if you directly assign SelectedValue it may through an exception if it is not exist in the list like bellow.