添加项目到Sitecore Combobox

时间:2013-03-07 12:10:11

标签: c# sitecore sitecore6

我正在创建一个Sitecore Sheer UI向导,其中包含这样的标记

<WizardFormIndent>
   <GridPanel ID="FieldsAction" Columns="2" Width="100%" CellPadding="2">
      <Literal Text="Brand:" GridPanel.NoWrap="true" Width="100%" />
      <Combobox ID="Brand" GridPanel.Width="100%" Width="100%">
         <!-- Leave empty as I want to populate available options in code -->
      </Combobox>
   <!-- Etc. -->
</WizardFormIndent>

但我似乎找不到在旁边的代码中为组合框“Brand”添加选项的方法。有谁知道如何完成下面的代码?

[Serializable]
public class MySitecorePage : WizardForm
{
    // Filled in by the sheer UI framework
    protected ComboBox Brands;

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!Context.ClientPage.IsEvent)
        {
             IEnumerable<Brand> brandsInSqlDb = GetBrands();

             // this.Brands doesn't seem to have any methods
             // to add options
        }
    }

}

3 个答案:

答案 0 :(得分:7)

首先,我假设您正在使用Sitecore.Web.UI.HtmlControls中的Sitecore Combobox(而不是Telerik控件)?

看着Reflector,它最终会做这样的事情:

foreach (Control control in this.Controls)
{
    if (control is ListItem)
    {
        list.Add(control);
    }
}

所以我希望你需要通过brandsInSqlDb构建一个循环,实例化一个ListItem并将其添加到你的Brands Combobox中。像

foreach (var brand in brandsInSqlDb)
{
    var item = new ListItem();
    item.Header = brand.Name; // Set the text
    item.Value = brand.Value; // Set the value

    Brands.Controls.Add(item);
}

答案 1 :(得分:1)

它应该是小写的 B (Combobox不是ComboBox)。完整命名空间是:

protected Sitecore.Web.UI.HtmlControls.Combobox Brands;

然后你可以添加选项,例如:

ListItem listItem = new ListItem();
this.Brands.Controls.Add((System.Web.UI.Control) listItem);
listItem.ID = Sitecore.Web.UI.HtmlControls.Control.GetUniqueID("ListItem");
listItem.Header = name;
listItem.Value = name;
listItem.Selected = name == selectedName;

答案 2 :(得分:0)

我这样做的方法是先从页面访问Combo框:

ComboBox comboBox = Page.Controls.FindControl("idOfYourComboBox") as ComboBox

现在您可以访问您在页面中定义的控件。现在你所要做的就是为它赋值:

 foreach (var brand in brandsInSqlDb)
{
    comboBox .Header = brand.Name; // Set the text
    comboBox .Value = brand.Value; // Set the value
    Brands.Controls.Add(item);
}