错误:foreach语句无法对“System.Web.UI.WebControls.ListItem”类型的变量进行操作 [英] Error: foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.ListItem'

查看:80
本文介绍了错误:foreach语句无法对“System.Web.UI.WebControls.ListItem”类型的变量进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个所选项目从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);
           }
       }

推荐答案

请查看此链接。可以帮到你。



错误:foreach语句无法对变量进行操作。 ..
Please check this link.May be helped to you.

Error: foreach statement cannot operate on variables of...


你不能在应该是集合的单个项目上使用foreach语句,但是你可以为你的解决方案使用for循环。像



.aspx Page



You can't use foreach statement on single item that should be a collection but you can use for loop for your solution.like

.aspx Page

<%@ 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]);
                }
            }
        }

    }
}


试试这个。 。



Try this..

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”类型的变量进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆