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; } 
} 
提供一个转换器