2012-06-29 96 views
1

我有一個枚舉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> 

但我的驗證是接受「請指定」作爲城市名稱。如果城市未選中,我想停止提交用戶。

回答

2

在添加枚舉項之前添加一個請指定。

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

取下枚舉明確指定時

+0

但我救了我枚舉值作爲tinyint在我的數據庫中。如果我從枚舉中刪除了「請指定」,它將在我的數據庫中爲奧克蘭節省0,並且我不希望那個 – KillerGearz

+0

好,你可以從1開始枚舉1'Auckland = 1' – tsukimi

+0

我做到了這一點,它適用於所有值除「北帕默斯頓」外。通過視覺工作室通過Jscript錯誤驗證碼給出如下:Microsoft JScript運行時錯誤:Sys.WebForms.PageRequestManagerServerErrorException:'TypeDropDownList'有一個SelectedValue是無效的,因爲它不存在於項目列表中。 參數名稱:值 – KillerGearz

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 
    } 
} 
+0

什麼是更清潔的解決方案? – tsukimi

+0

沒有通過Jscript錯誤工作:Microsoft JScript運行時錯誤:Sys.WebForms.PageRequestManagerServerErrorException:'TypeDropDownList'具有無效的SelectedValue,因爲它不存在於項目列表中。 參數名稱:值 – KillerGearz

+0

@tsukimi - 分配一個屬性,如System.ComponentModel.DataAnnotations.KeyAttribute,設置時,使用該值作爲值 – Polity

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>