【发布时间】:2014-04-01 03:46:42
【问题描述】:
lblSelected.Text = string.Empty; // empty the label that says you already have an item
foreach (GridViewRow row in gvSnacktastic.Rows) // check all the items in the grid view
{
CheckBox checkItOut = row.Cells[0].Controls[0] as CheckBox; // get the checkbox
if (checkItOut != null && checkItOut.Checked) // if the checkbox exists and is checked
{
bool storeVariable = true; // store a variable that tells us if we need to add it to the list
**foreach(ListBoxItem listItem in lbSelected.Items)** // Loop through list box items to see if we already have it. i haven't used listbox in a long time, this might be slightly wrong
{
// I'm not sure if it's .Text - compare the text of the listbox item to the checkbox item description
if(listItem.Text== checkItOut.Text)
{
lblSelected.Text = "The Item " + listItem.Text + " has already been added."; // make our already have it label
storeVariable = false; // remember that we don't need to add this item
}
}
if(storeVariable) // if we do need to add this item
{
lbSelected.Items.Add(checkItOut.Text); // create a new list box item with the check box item's description - this code is not complete
}
}
}
}
在我调试时,突出显示的 (**) 区域正在读取错误。知道我做错了什么吗?
我确信这是一个很容易回答的问题,但我在网上找不到正确的信息。
【问题讨论】:
-
ListBoxItem 下面有一个红色的波浪线。
-
我应该把它叫做 ListBox 吗?
-
lbSelected 在哪里声明和填充。
-
在设计视图@TheEdge
-
是否在模板项中?你能发布必要的 ASPX 代码吗?
标签: c# asp.net sql foreach listboxitem