Tuesday, March 20, 2012

How to use Description with an Enum Key in asp.net?

What happened if we have to bind a dropdownlist from Enum which holds Titles like Mr and Mrs but we have to show the textvaluefield like "Mr." and "Mrs."? Ok for this type of requirement we can use description attribute with the Key of a particular Enum, Here I am showing how to do that.

/// <summary>
        /// Enumeration TitleTypes use for getting the Titles
        /// Here I use DescriptionAttribute for holding the "Mr." Because "." is not supported in variable declaration
        /// </summary>
        public enum TitleTypes
            /// <summary>
            /// Title As Mr and DescriptionAttribute set to Mr.
            /// </summary>
            Mr = 1,

            /// <summary>
            /// Title As Mrs and DescriptionAttribute set to Mrs.
            /// </summary>
            Mrs = 2


No comments:

Post a Comment