Forum Moderators: open
FillValue(ddlSTATE,strstate );
/// <summary>
/// Takes already populated dropdown and selects the item in the list that matches the value
/// you passed in
/// </summary>
/// <param name="DropDown">dropdown which you want to select an item in</param>
/// <param name="Value">the value you wish to have selected in the dropdown</param>
static public void FillValue(System.Web.UI.WebControls.DropDownList DropDown, string Value)
{
//Deselects anything that is currently selected.
if (DropDown.SelectedIndex >=0)
{
DropDown.SelectedItem.Selected =false;
}
if (Value==null)
{
Value =string.Empty;
}
else
{
Value = Value.Trim();
}
//this findbyvalue is case sensitive
if (DropDown.Items.FindByValue(Value) != null)
{
DropDown.Items.FindByValue(Value).Selected = true;
}
else
{
if (Value != string.Empty)
{
//doing this to save a few cpu cycles. because it will be used in a loop
string lValue = Value.ToLower();
//this for loop will look for the value no mater the upper /lower case
for (int Count =0;Count <= DropDown.Items.Count -1;Count ++)
{
if (DropDown.Items[Count].Value.ToLower().Equals(lValue) ==true)
{
DropDown.Items[Count].Selected =true;
//this will exit us out of the function gracefully
return;
}
}
//Creates the Item in the List if it doesn't already
//exist.
DropDown.Items.Add(Value);
DropDown.Items.FindByValue(Value).Selected = true;
}
}
}