2015-09-23 33 views
0

我的頁面上有一個如下項目列表。以下顯示的所有名稱都是Sitecore中的項目。Sitecore:從sitecore中查找checkBoxList中選中值的項目

enter image description here

現在我要檢查用戶已選擇該項目,然後點擊一個按鈕讓他們的IDS(Sitecore的產品ID)。通過下面提到的代碼,我可以得到這個名字,但我如何獲得選定值的Id(sitecore項目ID)?更

// a temporary string to store the selected values 
string values = ""; 

// A loop to check if each checkbox is selected then get the value 
foreach (ListItem objItem in cblFoodItems.Items) 
{ 
    if (objItem.Selected) 
    { 
     values += objItem.Value + ","; 
    } 
} 

有點如果你想知道,我如何顯示在checkBoxList的項目細節。

<asp:CheckBoxList runat="server" ID="cblFoodItems" RepeatColumns="4"/> 

代碼隱藏

Item foodFldr = Sitecore.Context.Database.GetItem("{42808F4D-5335-4BB6-911B-9B79E50CFE99}"); 

foreach (var foodItem in foodFldr.Children) 
{ 
    var newListItem = new ListItem(foodItem.Name); 
    cblFoodItems.Items.Add(newListItem); 
} 

回答

3

更改您的代碼背後:

foreach (var foodItem in foodFldr.Children) 
{ 
    var newListItem = new ListItem(foodItem.Name, foodItem.ID.ToString()); 
    cblFoodItems.Items.Add(newListItem); 
} 

它仍然會顯示您的項目到最終用戶的Name,但價值會ID而不是項目名稱。

+0

謝謝你!我檢查了你的代碼,現在我得到了ID。 – Kamran

相關問題