Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
i created enum with multiple values. im using the enum value as label text. im apply text for label its working fine at the same im trying to use it for errormessage or checkbox text its not binded. please let me know the fix:

Enum:
C#
public enum ResourceTexts
   {
       [EnumDisplayName(DisplayName = "Next")]
       btnNext ,


}


ASP.NET
<asp:Label ID="lblHowReceived" runat="server"><%=ResourceTexts.btnNext.DisplayName()%></asp:Label>


Output: Next

C#
ErrorMessage="<%=ResourceTexts.rfvMobileNumber.DisplayName()%>"



<asp:CheckBox ID="chkAddFrequency" runat="server" Text="<%=ResourceTexts.btnNext1.DisplayName()%>"
CssClass="Radio" />

Output: "
ASP.NET
<%=ResourceTexts.btnNext1.DisplayName()%>

"

What I have tried:

HTML
<asp:CheckBox ID="chkAddFrequency" runat="server" Text="<%=ResourceTexts.btnNext1.DisplayName()%>"
                                CssClass="Radio" />
<asp:CheckBox ID="chkAddFrequency" runat="server" Text="<%ResourceTexts.btnNext1.DisplayName()%>"
                                CssClass="Radio" />
<asp:CheckBox ID="chkAddFrequency" runat="server" Text='<%=ResourceTexts.btnNext1.DisplayName()%>'
                                CssClass="Radio" />
<asp:CheckBox ID="chkAddFrequency" runat="server" Text="<%ResourceTexts.btnNext1.DisplayName()%>"
                                CssClass="Radio" />
Posted
Updated 22-Sep-16 23:45pm
v3

1 solution

try this

C#
public enum ResourceTexts
  {
      [EnumDisplayName(DisplayName = "Next")]
      btnNext,
      [EnumDisplayName(DisplayName = "Previous")]
      btnPrev,
  }

  [System.AttributeUsage(System.AttributeTargets.Field)]
  public class EnumDisplayName : System.Attribute
  {
      public string DisplayName { get; set; }

  }
  public static class MyExtension
  {
      public static string GetDisplayName(this Enum enumValue)
      {
          return ((EnumDisplayName)enumValue.GetType().GetMembers().First(k => k.Name == enumValue.ToString()).GetCustomAttributes(typeof(EnumDisplayName), true)[0]).DisplayName;
      }
  }



HTML
Text="<%# ResourceTexts.btnNext.GetDisplayName() %>" 
 
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