如何将枚举类型的下拉列表绑定到下拉列表?

时间:2012-06-19 11:02:41

标签: c# asp.net sharepoint-2010

public enum CandidateStatus : int {

    None = 0,

    Invalid = 1,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Pending Approval")]
    PendingApproval = 2,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Open")]
    Open = 4,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Screening")]
    Screening = 8,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Interviewing")]
    Interviewing = 16,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Offers Issued")]
    OffersIssued = 32,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Hired")]
    Hired = 64,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Cancelled")]
    Cancelled = 128,
}

这是我在linq.cs文件中的枚举代码。

此处CandidateStatus是Sharepoint列表中定义的选择列。 如何在下拉列表中绑定“CandidateStatus”的这个选择字段,如何将此选定的下拉值插入Sharepoint列表? 请你帮忙解决这个问题......

1 个答案:

答案 0 :(得分:2)

我认为您唯一的选择是将枚举值映射到对象:

myDropDown.TextField = "Value";
myDropDown.ValueField = "ID";

myDropDown.DataSource = ((CandidateStatus []) Enum.GetValues(typeof(CandidateStatus))
   .Select(c => new {
                      ID = (int)c, 
                      Value = ( typeof(CandidateStatus)
                       .GetField(c.ToString())
                       .GetCustomAttributes(
                         typeof(Microsoft.SharePoint.Linq.ChoiceAttribute), false) 
                           as EnumStringValueAttribute[]).FirstOrDefault() 
                        ?? c.ToString()
                    });
myDropDown.DataBind();