coders,
It's my first question here. So if it lacks of some stackoverflow pattern, pardon me, i'll edit it as you claim.
All I need: Convert my enums to a collection(the one I've came out to use was the dictionary) but ignoring the enum main value itself... I just want the attribute(as XmlEnum) and the CHAR value. So I can populate my drop down list and be happy.
Whatever code:
// whatever.aspx
<asp:DropDownList ID="ddlGreekTeam" runat="server" ... whatever />
<asp:DropDownList ID="ddlMilitaryTeam" runat="server" ... whatever />
// whatever.aspx.cs
private void OnPageLoad()
{
PopulateTeamDropDownLists<GreekTeam>(ddlGreekTeam)
PopulateTeamDropDownLists<MilitaryTeam>(ddlMilitaryTeam)
// ... whatever
}
private void PopulateTeamDropDownLists<TEnum>(DropDownList ddl)
{
ddl.Items.Clear();
ddl.Items.Insert(0, new ListItem("All", null));
foreach(var team in ListTeams<TEnum>())
ddl.Items.Add(new ListItem(team.Value.ToString(), team.Key));
}
// method needed
public IDictionary<string, char> ListTeams<TEnum>()
{
// TODO: the magic I'm looking for...
}
// enums
public enum GreekTeam
{
[XmlEnum("Alpha Team")]
Alpha = 'A',
[XmlEnum("Beta Team")]
Beta = 'B',
[XmlEnum("Chi Team")]
Gamma = 'G',
// ... whatever
}
public enum MilitaryTeam
{
[XmlEnum("Alpha Team")]
Alpha = 'A',
[XmlEnum("Bravo Team")]
Bravo = 'B',
[XmlEnum("Charlie Team")]
Charlie = 'C',
// ... whatever
}
OUTPUT:
<select>
<option value="A">Alpha Team</option>
<option value="B">Beta Team</option>
<option value="G">Gamma Team</option>
...
</select>
<select>
<option value="A">Alpha</option>
<option value="B">Bravo Team</option>
<option value="C">Charlie Team</option>
...
</select>