public virtual void Bind(object control, IEnumerable values, params object[] p)
{
if (values == null) return;
var ddl = control as DropDownList;
if (ddl == null) return;
ddl.Items.Clear();
var selectval = string.Empty;
if (p != null && p.Length > 0) selectval = (p[0] is IDomainObject) ? (p[0] as IDomainObject).Id.ToString() : p[0].ToString();
foreach (var value in values)
{
var text = (value is Enum) ? (value as Enum).GetDescription() : value.ToString();
var val = (value is IDomainObject) ? (value as IDomainObject).Id.ToString() : text;
var listItem = new ListItem(text, val);
if (p != null && p.Length > 0)
{
listItem.Selected = val == selectval;
}
ddl.Items.Add(listItem);
}
if (p != null && p.Length > 0)
{
if (ddl.SelectedValue.IsNullOrEmpty() && !p[0].ToString().IsNullOrEmpty())
ddl.Items.Insert(0, new ListItem("<invalid value>") { Selected = true });
}
}