Supposing to have the follow enumerator:
public enum UserRole
{
Guest = 0,
StandardUser = 1,
Administrator = 2
}
And a Web Form with a DropDownList (named "ddlUserRole"):
<form runat="server">
<asp:DropDownList
ID="ddlUserRole"
runat="server"
OnSelectedIndexChanged="ddlUserRole_SelectedIndexChanged"
AutoPostBack="true" />
</form>
We can simply bind the enumerator to the DropDownList with a couple of line of code:
protected void Page_Load(object sender, EventArgs e)
{
comboBox1.DataSource = Enum.GetValues(typeof(UserRole));
comboBox1.DataBind();
}
private void ComboBox1ValueChanged(object sender, EventArgs e)
{
// retrieve selected user role from the DropDownList:
UserRole r = (UserRole)this.comboBox1.SelectedValue;
}
HTH