簡體   English   中英

將項目添加到Sitecore組合框

[英]Add Items To Sitecore Combobox

我正在創建一個具有這樣標記的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
        }
    }

}

首先,我假設您使用的是Sitecore.Web.UI.HtmlControls(而不是Telerik控件)中的Sitecore組合框?

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

它應該是小寫字母B (組合框而不是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;

我的方法是從頁面1中訪問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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM