1

我正在構建一個擴展BaseUserControl的複雜WFFM用戶控件。此控件具有多個根據某些業務邏輯預填充的字段。其中一個字段應該是一個下拉菜單,顯示一系列Sitecore項目的值。這裏是我的ListField屬性的定義:Sitecore 6 WFFM:ListField值?

private string myListField; 
[VisualProperty("My List Field:", 100), 
    VisualCategory("Appearance"), VisualFieldType(typeof(ListField))] 
public string MyListField{ 
    get { return myListField; } 
    set { myListField= value; } 
} 

當我調試此,titleFieldList的內容是包含在URL下面的XML編碼格式的字符串:

%3Cquery%20噸%3D%22root %22%20vf%3D%22__ID%22%20tf%3D%22Value%22%3E%3Cvalue%3E%7B814FC177-2750-48D6-B7B7-4EE87012C637%7D%3C%2Fvalue%3E%3C%2Fquery%3E

其中,解碼是:

<query t="root" vf="__ID" tf="Value"> 
    <value>{814FC177-2750-48D6-B7B7-4EE87012C637}</value> 
</query> 

我明白這個XML的含義。它說,ID爲Guid的項目的所有孩子都被用來填充我的列表,使用模板字段「__ID」作爲值,模板字段「值」作爲文本。 有人可以幫我理解我該怎麼做綁定一個asp:DropDownList到這個?這是一個已被序列化和編碼的特定sitecore對象嗎? 有沒有可以處理這個問題的sc:control?

謝謝!

**編輯**

所以,我想下面的代碼

string encodedQuery = TitleFieldList; 
string query = HttpUtility.UrlDecode(encodedQuery); 
XDocument xmlQuery = XDocument.Parse(query); 
if (xmlQuery.Element("query") != null) 
{ 
    Dictionary<string, string> nodesDictionary = new Dictionary<string, string>(); 
    string root = xmlQuery.Element("query").Element("value").Value; 
    string value = xmlQuery.Element("query").Attribute("vf").Value; 
    string text = xmlQuery.Element("query").Attribute("tf").Value; 

    Item rootItem = SitecoreUtility.GetItemWithoutSecurity(new ID(root)); 
    ChildList childList = rootItem.GetChildren(); 
    foreach (Item child in childList) 
    { 
     string theValue = (value == "__ID") ? child.ID.ToString() : child.Fields[value].ToString(); 
     string theText = child.Fields[text].ToString(); 
     nodesDictionary.Add(theText, theValue); 
    } 

    titleDropDownList.DataSource = nodesDictionary; 
    titleDropDownList.DataTextField = "key"; 
    titleDropDownList.DataValueField = "value"; 
    titleDropDownList.DataBind(); 
} 

和它的作品。下拉列表中填入來自編輯器中選定字段的正確數據。我無法相信沒有更簡單的方法來做到這一點。另外,我應該如何尊重MultipleSelectedValueField和EmptyChoiceField(如果存在)?

回答

2

嘗試更改屬性的返回類型並添加屬性TypeConverter。 TypeConverter中指定的類型負責將原始字符串值轉換爲屬性的返回類型。

ListItemCollectionConverter - 是由WFFM

[VisualProperty("My List Field:", 100)] 
[VisualCategory("Appearance")] 
[VisualFieldType(typeof(ListField))] 
[TypeConverter(typeof(Sitecore.Form.Web.UI.Controls.ListItemCollectionConverter.ListItemCollectionConverter))] 
public Sitecore.Form.Web.UI.Controls.ListItemCollection MyListField{ 
    get { return myListField; } 
    set { myListField= value; } 
} 
提供一個轉換器