ASP.NET枚举下拉列表验证

时间:2012-06-29 02:48:03

标签: c# asp.net

我有一个枚举

public enum TypeDesc
{
[Description("Please Specify")]
PleaseSpecify,
Auckland,
Wellington,
[Description("Palmerston North")]
PalmerstonNorth,
Christchurch
}

我使用page_Load上的以下代码将此枚举绑定到下拉列表

protected void Page_Load(object sender, EventArgs e)
        {
            if (TypeDropDownList.Items.Count == 0)
            {
                foreach (TypeDesc newPatient in EnumToDropDown.EnumToList<TypeDesc>())
                {
                 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 && attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }

        public static IEnumerable<T> EnumToList<T>()
        {
            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<T> enumValList = new List<T>(enumValArray.Length);

            foreach (int val in enumValArray)
            {
                enumValList.Add((T)Enum.Parse(enumType, val.ToString()));
            }

            return enumValList;
        }

我的aspx页面使用以下代码验证

            <asp:DropDownList ID="TypeDropDownList" runat="server" >
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="TypeRequiredValidator" runat="server" ControlToValidate="TypeDropDownList" ErrorMessage="Please Select a City" Text="<img src='Styles/images/Exclamation.gif' />"
                ValidationGroup="city"></asp:RequiredFieldValidator>

但我的验证是接受“请指定”作为城市名称。如果未选择城市,我想停止用户提交。

3 个答案:

答案 0 :(得分:2)

在添加枚举项之前添加一个请指定。

 TypeDropDownList.Items.Add("Please Specify","");
 foreach (TypeDesc newPatient in EnumToDropDown.EnumToList<TypeDesc>())
 {
    TypeDropDownList.Items.Add(EnumToDropDown.GetEnumDescription(newPatient), newPatient.ToString());
 }

从枚举

中删除“请指定”

答案 1 :(得分:0)

DropdownList可以在明确指定时绑定到ValueText属性。当一个项的值为null时,验证器将会选择它。

<asp:DropDownList ID="TypeDropDownList" runat="server" DataTextField="Text" DataValueField="Value" ></asp:DropDownList>

并在添加项目时:

foreach (TypeDesc newPatient in EnumToDropDown.EnumToList<TypeDesc>())
{
   string text = EnumToDropDown.GetEnumDescription(newPatient)),
   TypeDropDownList.Items.Add(new
   {
       Text = text,
       Value = text == "Please specify" ? null : text // should be replaced with a clean solution
   }
}

答案 2 :(得分:0)

我整理出来后使用了以下代码

if (TypeDropDownList.Items.Count == 0)
            {
                foreach (TypeDesc newPatient in EnumToDropDown.EnumToList<TypeDesc>())
                {
                    string text = EnumToDropDown.GetEnumDescription(newPatient);

                    TypeDropDownList.Items.Add(new ListItem(text, newPatient.ToString()));
                }
            }

和验证器

<asp:RequiredFieldValidator ID="TypeRequiredValidator" runat="server" ControlToValidate="TypeDropDownList"
                InitialValue="PleaseSpecify" ErrorMessage="Please Select a City" Text="<img src='Styles/images/Exclamation.gif' />"
                ValidationGroup="city"></asp:RequiredFieldValidator>