问题描述
我正在尝试将多个所选项目从onelist框移动到另一个列表框,使用此代码但不知何故它显示错误 foreach语句无法对类型为'System.Web.UI.WebControls.ListItem'的变量进行操作因为'System.Web.UI.WebControls.ListItem'不包含'GetEnumerator'的公共定义
i不知道为什么会出现这个错误..?
请帮我修理它提前谢谢..
我的代码是
i am trying to move multiple selected item from onelist box to another listbox,using this code but somehow it shows a error "foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.ListItem' because 'System.Web.UI.WebControls.ListItem' does not contain a public definition for 'GetEnumerator' "
i don't know why this error occurs..?
please help me to fix it Thanks in advance..
my code is
protected void imgbtnMoveRightListBox_Click(object sender, ImageClickEventArgs e)
{
foreach (var item in lstboxSkill.SelectedItem)
{
lstBBoxSkill2.Items.Add(item);
}
}
推荐答案
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="envornment.aspx.cs" Inherits="TestApplication.envornment" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListBox Id="lstboxSkill" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="1" Text="ABC"></asp:ListItem>
<asp:ListItem Value="2" Text="XYZ"></asp:ListItem>
</asp:ListBox>
<asp:ImageButton ID="imgbtnMoveRightListBox" runat="server" ImageUrl="demo.gif"
onclick="imgbtnMoveRightListBox_Click" />
<asp:ListBox ID="lstBBoxSkill2" runat="server" SelectionMode="Multiple">
</asp:ListBox>
</form>
</body>
</html>
.cs页面
.cs page
using System;
using System.Web.UI.WebControls;
namespace TestApplication
{
public partial class envornment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void imgbtnMoveRightListBox_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
for (int i = 0; i < lstboxSkill.Items.Count; i++)
{
if (lstboxSkill.Items[i].Selected)
{
lstBBoxSkill2.Items.Add(lstboxSkill.Items[i]);
}
}
}
}
}
foreach (ListItem item in lstLeft.Items)
{
if (item.Selected)
{
lstRight.Items.Add(new ListItem() { Text = item.Text, Value = item.Value });
}
}
这篇关于错误:foreach语句无法对“System.Web.UI.WebControls.ListItem”类型的变量进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!