Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET enum dropdownlist validation
    primarykey
    data
    text
    <p>I have got a enum</p> <pre><code>public enum TypeDesc { [Description("Please Specify")] PleaseSpecify, Auckland, Wellington, [Description("Palmerston North")] PalmerstonNorth, Christchurch } </code></pre> <p>I am binding this enum to drop down list using the following code on page_Load</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { if (TypeDropDownList.Items.Count == 0) { foreach (TypeDesc newPatient in EnumToDropDown.EnumToList&lt;TypeDesc&gt;()) { TypeDropDownList.Items.Add(EnumToDropDown.GetEnumDescription(newPatient)); } } } public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null &amp;&amp; attributes.Length &gt; 0) return attributes[0].Description; else return value.ToString(); } public static IEnumerable&lt;T&gt; EnumToList&lt;T&gt;() { Type enumType = typeof(T); // Can't use generic type constraints on value types, // so have to do check like this if (enumType.BaseType != typeof(Enum)) throw new ArgumentException("T must be of type System.Enum"); Array enumValArray = Enum.GetValues(enumType); List&lt;T&gt; enumValList = new List&lt;T&gt;(enumValArray.Length); foreach (int val in enumValArray) { enumValList.Add((T)Enum.Parse(enumType, val.ToString())); } return enumValList; } </code></pre> <p>and my aspx page use the following code to validate</p> <pre><code> &lt;asp:DropDownList ID="TypeDropDownList" runat="server" &gt; &lt;/asp:DropDownList&gt; &lt;asp:RequiredFieldValidator ID="TypeRequiredValidator" runat="server" ControlToValidate="TypeDropDownList" ErrorMessage="Please Select a City" Text="&lt;img src='Styles/images/Exclamation.gif' /&gt;" ValidationGroup="city"&gt;&lt;/asp:RequiredFieldValidator&gt; </code></pre> <p>But my validation is accepting "Please Specify" as city name. I want to stop user to submit if the city is not selected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload