Creating dropdown through enum

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.
 
B

Ben Voigt [C++ MVP]

Sampat said:
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;


try this instead:

int enumIndex = (int)(IdeaType)Enum.Parse(typeof(IdeaType), item);
 
S

Sampat

try this instead:

int enumIndex = (int)(IdeaType)Enum.Parse(typeof(IdeaType), item);







- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks, that works. Now I have am facing one more issue. While looping
it always picks the items with the lowest value first i.e Third comes
first as the value is assigned as 1 and then then First so on...can I
also maintain the sequence as what is specified in the enum while
creating the dropdown.
P.S: I have some restrictions due to which I cannot modify the
sequence or the value assigned.
 
B

Ben Voigt [C++ MVP]

Sampat said:
Thanks, that works. Now I have am facing one more issue. While looping
it always picks the items with the lowest value first i.e Third comes
first as the value is assigned as 1 and then then First so on...can I
also maintain the sequence as what is specified in the enum while
creating the dropdown.
P.S: I have some restrictions due to which I cannot modify the
sequence or the value assigned.

I don't think that information is stored in the metadata. The debug
database probably contains the line number where each member was declared,
with would give the original source code order, but I wouldn't recommend
using that either.

You could place attributes on each item to determine the order. A bit of a
maintenance headache if the sequence ever changes, but you said it's locked
down so maybe not a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top