1

I am building a complex WFFM user control that extends BaseUserControl. This control has multiple fields that get prepopulated based on some business logic. One of the fields is supposed to be a drop down which shows values from a series of Sitecore items. Here is the definition of my ListField property:

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

When I debug this, the content of titleFieldList is a string that contains the following XML in URL encoded format:

%3Cquery%20t%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

which, decode, is:

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

I understand the meaning of this XML. It says that all the children of the item whose ID is that Guid are supposed to be used to populate my list, using the template field "__ID" for the value and the template field "value" for the text. Can someone help me understand what am I supposed to do to bind an asp:DropDownList to this? Is this a particular sitecore object that has been serialized and encoded? Is there an sc:control that can handle this?

Thanks!

** EDIT **

So I tried the following piece of code

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();
}

and it works. The dropdownlist is populated with the correct data coming from the fields that were selected in the editor. I just can't believe that there is no easier way to do this. Plus how am I supposed to honor the MultipleSelectedValueField and the EmptyChoiceField if present?

4

1 に答える 1

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; } 
}
于 2012-11-14T12:26:30.797 に答える