I have a list of items as follows on my page. All the names shown below are items in Sitecore.
Now I want to check which items the user has selected and then get their ids(sitecore Item ID) on a button click. With the below mentioned code I can get the name but how I can get the Id (sitecore Item ID) of selected values?
// 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 + ",";
}
}
A bit more detail in case you like to know, how I am showing the items in the checkBoxList
.
<asp:CheckBoxList runat="server" ID="cblFoodItems" RepeatColumns="4"/>
Code Behind
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);
}
Change your code behind to:
foreach (var foodItem in foodFldr.Children)
{
var newListItem = new ListItem(foodItem.Name, foodItem.ID.ToString());
cblFoodItems.Items.Add(newListItem);
}
It will still display the Name
of your item to the end users, but the value will be ID
instead of item name.