Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, Below is a enum i created.
C#
public enum DialEntries
      {
          [Description("LastName + FirstName or FirstName + LastName")] LastNameFirstNameorFirstNameLastName,
          [Description("LastName + FirstName")]LastNameFirstName
      }


Now i want to fill a ddl with the description fo the enum like this


C#
strHTML.Append("<select id='ddlDialEntries' name='ddlDialEntries'>");
strHTML.Append("<option value=" + ServiceAssignmentModel.DialEntries.LastNameFirstName + "> " + ServiceAssignmentModel.DialEntries.LastNameFirstName + "</option>");
strHTML.Append("<option value=" + ServiceAssignmentModel.DialEntries.LastNameFirstNameorFirstNameLastName + "> " + ServiceAssignmentModel.DialEntries.LastNameFirstNameorFirstNameLastName + "</option>");
                strHTML.Append("</select>");
                return strHTML.ToString();


Where the displayed text should be the description.. Pls help
Posted
Updated 25-Mar-13 1:20am
v2
Comments
Arjun Menon U.K 25-Mar-13 7:29am    
strHTML.Append("<select id='ddlDialEntries' name='ddlDialEntries'>");

FieldInfo fi = ServiceAssignmentModel.DialEntries.LastNameFirstName.GetType().GetField(ServiceAssignmentModel.DialEntries.LastNameFirstName.ToString());

DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

if (attributes != null && attributes.Length > 0)
description= attributes[0].Description;
else
description= ServiceAssignmentModel.DialEntries.LastNameFirstName.ToString();

strHTML.Append("<option value=" + ServiceAssignmentModel.DialEntries.LastNameFirstName + "> " + description + "</option>");

fi = ServiceAssignmentModel.DialEntries.LastNameFirstNameorFirstNameLastName.GetType().GetField(ServiceAssignmentModel.DialEntries.LastNameFirstNameorFirstNameLastName.ToString());

attributes =(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

if (attributes != null && attributes.Length > 0)
description = attributes[0].Description;
else
description= ServiceAssignmentModel.DialEntries.LastNameFirstNameorFirstNameLastName.ToString();

strHTML.Append("<option value=" + ServiceAssignmentModel.DialEntries.LastNameFirstNameorFirstNameLastName + "> " + description1 + "</option>");

strHTML.Append("</select>");
return strHTML.ToString();

I was able to achieve with the code above... but am not sure its a good method or not??

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900