我正在构建一个扩展 BaseUserControl 的复杂 WFFM 用户控件。此控件具有多个字段,这些字段根据某些业务逻辑进行预填充。其中一个字段应该是一个下拉菜单,显示一系列 Sitecore 项目的值。这是我的 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%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%2F查询%3E
其中,解码是:
<query t="root" vf="__ID" tf="Value">
<value>{814FC177-2750-48D6-B7B7-4EE87012C637}</value>
</query>
我理解这个 XML 的含义。它表示 ID 为 Guid 的项目的所有子项都应该用于填充我的列表,使用模板字段“__ID”作为值,使用模板字段“值”作为文本。有人可以帮我理解我应该怎么做才能将 asp:DropDownList 绑定到这个吗?这是一个已序列化和编码的特定站点核心对象吗?是否有 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?