Enum vs ListManagerService Methodology

Enums drive logic. ListManagerService drives dropdown list items that can be freely added with no code change. aka data.

Advertisements

How To Bind Enum To DropDownList

This will set the dropdown text to the enum’s description and the value (int) from the enum.

SeverityType.cs (Enum)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace com.aep.ehap.bl
{
    /// <summary>
    /// Defines a type of Severity.
    /// </summary>
    public enum SeverityType
    {
        [Description("1 - Injury free event, first aid, non recordable")]
        InjuryFreeEvent = 0,

        [Description("2 - Medical, restricted duty/transfer = <2 days")]
        MedResDutyLessTwoDays = 1,

        [Description("3 - Restricted duty/transfer = <= 10 days")]
        MedResDutyLessTenDays = 2,

        [Description("4 - Restricted duty/transfer = <30 days")]
        MedResDutyLessThirtyDays = 3,

        [Description("5 - Restricted duty/transfer =<60 days")]
        MedResDutyLessSixtyDays = 4,

        [Description("6 - Complete disabling or fatality")]
        DisablingOrFatality = 5

    }
}

Page CodeBehind

          
            // Bind the Dropdown lists

            // Default first value in dropdown
            this.ddlPotentialSeverity.Items.Add(new ListItem() { Text = "Select one...", Value = "-1" });

            // Retrieve list of SeverityType's from enum
            List<SeverityType> SeverityTypes = new List<SeverityType>((SeverityType[])Enum.GetValues(typeof(SeverityType)));

            this.ddlPotentialSeverity.Items.AddRange(
                 (from severityType in SeverityTypes
                  select new ListItem()
                  {
                      Value = severityType.ToString(),
                      Text = Util.GetEnumDescription(severityType)
                  }).ToArray());

Util.cs (GetEnumDescription)


        /// <summary>
        /// Gets the enum description given enum value
        /// </summary>
        /// <param name="value">The value from enum.</param>
        /// <returns></returns>
        public static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes =
                  (DescriptionAttribute[])fi.GetCustomAttributes(
                  typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }

Extract Description From Enum

C#

using System.ComponentModel;
using System.Reflection;

...
..
.

            //  instantiate List of enum and add to it
            List<InvestorEmailAlertType> subscriptions = new List<InvestorEmailAlertType>();
            subscriptions.Add(InvestorEmailAlertType.AllCorporateNewsReleases);
            subscriptions.Add(InvestorEmailAlertType.FinancialNewsReleasesOnly);

          // Create another List of type string to hold Enum Descriptions
            List<string> subscriptionDesc = new List<string>();

            foreach (InvestorEmailAlertType subscriptionChoice in subscriptions)
            {
                subscriptionDesc.Add(GetEnumDescription(subscriptionChoice));
            }


    public static string GetEnumDescription(Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());
        DescriptionAttribute[] attributes =
              (DescriptionAttribute[])fi.GetCustomAttributes(
              typeof(DescriptionAttribute), false);
        return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
    }

InvestorEmailAlertType Enum

using System;
using System.ComponentModel;


public enum InvestorEmailAlertType
{
    [Description("All corporate news releases")]
    AllCorporateNewsReleases,

    [Description("Financial news releases only")]
    FinancialNewsReleasesOnly,

    [Description("Environmental news releases only")]
    EnvironmentalNewsReleasesOnly,

    [Description("Operations news releases only")]
    OperationsNewsReleasesOnly,

    [Description("All regional utility news releases")]
    AllRegionalUtilityNewsReleases,

    [Description("Investor presentation updates")]
    InvestorPresentationUpdates,

    [Description("Investor calendar updates")]
    InvestorCalendarUpdates,

    [Description("Financial webcast alerts")]
    FinancialWebcastAlerts,

    [Description("All SEC Filings")]
    AllSECFilings,

    [Description("Quarterly and Annual Reports")]
    QuarterlyAndAnnualReports,

    [Description("Insider Transactions")]
    InsiderTransactions,

    [Description("Quote By Email")]
    QuoteByEmail

}

Bind ASP.NET Repeater to List of Enum’s

C#

            //  instantiate List of enum and add to it
            List<InvestorEmailAlertType> subscriptions = new List<InvestorEmailAlertType>();
            subscriptions.Add(InvestorEmailAlertType.AllCorporateNewsReleases);
            subscriptions.Add(InvestorEmailAlertType.FinancialNewsReleasesOnly);

            // Bind list to repeater
            RepeaterSubscriptions.DataSource = subscriptions;
            RepeaterSubscriptions.DataBind();

ASP.NET

            <asp:Repeater ID="RepeaterSubscriptions" runat="server">
                <ItemTemplate>
                    <ul>
                        <li>
                            <%# Container.DataItem %>
                        </li>
                    </ul>
                </ItemTemplate>
            </asp:Repeater>