Reputation: 1984
I have enum for Salutation like
public enum SALUTATION
{
MR = 1,
MS = 2,
MRS = 3,
}
and in my staff
class my Salutation
property is like,
public SALUTATION Salutation
{
get;
set;
}
here, while editing the staff profile am just binding the datas from the database. For salutation I just tried binding the salutation like
ddlSalutation.SelectedValue = Enum.GetName(typeof(SALUTATION), staff.Salutation);
but it binds the selectedValue as -1
always. how can I bind the exact value in the ddl selected item. can anyone help me here..
in the page load event am just binding the ddl source as
Hashtable hashSalutation = Utilities.GetEnumList(typeof(SALUTATION));
ddlSalutation.DataSource = hashSalutation;
ddlSalutation.DataTextField = "value";
ddlSalutation.DataValueField = "key";
ddlSalutation.DataBind();
ddlSalutation.Items.Insert(0, new ListItem("Select Salutation", "-1"));
public Hashtable GetEnumList(Type enumeration)
{
string[] names = Enum.GetNames(enumeration);
Array values = Enum.GetValues(enumeration);
Hashtable ht = new Hashtable();
for (int i = 0; i < names.Length; i++)
{
ht.Add(Convert.ToInt32(values.GetValue(i)).ToString(), names[i]);
}
return ht;
}
Upvotes: 0
Views: 2120
Reputation: 4327
First set this as your datasource
ddSalutation.DataSource = Enum.GetNames(typeof(Salutations));
Then for selected value
ddlSalutation.SelectedValue = staff.Salutation.ToString();
Upvotes: 1