S
Sampat
Hi,
I am trying to create a dropdown by looping through an enum and
assign the name as a dropdown item and value as the value assigned in
the enum for that item.I am able to either loop by name or by value
and I am not able to find a way to loop through an enum item. Below is
the code snippet:
Enum:
public enum IdeaType
{
First = 8,
Second = 12,
Third = 1,
Fourth = 23
}
My code to create dropdown:
//Build the dropdown html in the code so that any new addition in the
enum would be taken care easily.
StringBuilder strdropdown = new StringBuilder();
strdropdown.Append("<select id=\"comptype\" name=
\"comptype\">");
foreach (string item in
Enum.GetNames(typeof(IdeaType)))
{
int enumIndex = 0;
strdropdown.AppendFormat("<option value=
\"{0}\">{1}</option>",enumIndex,item);
enumIndex++;
}
strdropdown.Append("</select>");
The problem here is the enumIndex is been set at the start of the loop
and incremented within the loop and in this case I am not able to add
the correct value assigned to the enum item i.e the dropdown is
getting created as <option value="0">First</option> but I want it as
<option value="8">First</option>.
Any idea how to go about this problem?
Thanks,
Sampat.
I am trying to create a dropdown by looping through an enum and
assign the name as a dropdown item and value as the value assigned in
the enum for that item.I am able to either loop by name or by value
and I am not able to find a way to loop through an enum item. Below is
the code snippet:
Enum:
public enum IdeaType
{
First = 8,
Second = 12,
Third = 1,
Fourth = 23
}
My code to create dropdown:
//Build the dropdown html in the code so that any new addition in the
enum would be taken care easily.
StringBuilder strdropdown = new StringBuilder();
strdropdown.Append("<select id=\"comptype\" name=
\"comptype\">");
foreach (string item in
Enum.GetNames(typeof(IdeaType)))
{
int enumIndex = 0;
strdropdown.AppendFormat("<option value=
\"{0}\">{1}</option>",enumIndex,item);
enumIndex++;
}
strdropdown.Append("</select>");
The problem here is the enumIndex is been set at the start of the loop
and incremented within the loop and in this case I am not able to add
the correct value assigned to the enum item i.e the dropdown is
getting created as <option value="0">First</option> but I want it as
<option value="8">First</option>.
Any idea how to go about this problem?
Thanks,
Sampat.