【问题标题】:Simple ListBoxItem Issue, not sure how to write this into the code correctly简单的 ListBoxItem 问题,不确定如何正确将其写入代码
【发布时间】: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


【解决方案1】:

请更换

foreach(ListBoxItem listItem in lbSelected.Items)
{

}

foreach(ListItem listItem in lbSelected.Items)
  {

  }

【讨论】:

  • 我改变了这个,波浪线消失了。但是,我的选择过程不起作用。您是否愿意查看示例,然后查看我的代码,看看您是否能找出我做错了什么?为了名誉,我不想在未经许可的情况下到处乱扔链接。 @Kiranramchandran
  • stackoverflow.com/questions/22777191/… 在这里我给出了更好的描述。 @Kiranramchandran
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多