Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
C#
protected void imgbtnMoveRightListBox_Click(object sender, ImageClickEventArgs e)
       {
           foreach (var item in lstboxSkill.SelectedItem)
           {
               lstBBoxSkill2.Items.Add(item);
           }
       }
Posted
Comments
s#@!k 7-Feb-14 2:52am    
lstboxSkill.SelectedItem is a single item.It is causing problem use
lstboxSkill.Items is collection.

Please check this link.May be helped to you.

Error: foreach statement cannot operate on variables of...
 
Share this answer
 
v2
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

HTML
<%@ 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 page

C#
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]);
                }
            }
        }

    }
}
 
Share this answer
 
v2
Comments
Amit Kumar143 7-Feb-14 3:55am    
yes code is working but the problem is in lstboxSkill always first item is selected true and not the other items. It is not getting which items are selected except first .
so in second listbox the first item of first-listbox is added even when it is not selected.
Sandeep Singh Shekhawat 7-Feb-14 3:58am    
did you add SelectionMode="Multiple"
Amit Kumar143 7-Feb-14 4:02am    
yes
Sandeep Singh Shekhawat 7-Feb-14 4:07am    
Its working on my end please add this code in a new separate application and test it. I think you are mixing some thing on code behind on page load. Please debug your code.can you please paste your code?
Amit Kumar143 7-Feb-14 4:33am    
yes even i have tried it in another application and it is working well there,but don't know what is problem in my current application

in my application it reacquires to binding first listbox that is "lstboxSkill" from a dropdown selectedIndex change event
and fill second listbox from first listbox selected value

i am binding first list box using this methods
protected void ddlSkillType_SelectedIndexChanged(object sender, EventArgs e)
{
List<TblSkillType> lstSkillType = ServiceAccess.GetProxy().GetSkillType();
List<TblSkill> lstSkill = ServiceAccess.GetProxy().GetAllSkill();
List<TblSkillDetail> lstSkillDetails = ServiceAccess.GetProxy().GetAllSkillDtls();
var skill = (from a in lstSkillType
from b in lstSkill
from c in lstSkillDetails
where a.SkillTypeId == Convert.ToInt32(ddlSkillType.SelectedValue) && a.SkillTypeId == b.SkillTypeId && b.SkillId == c.SkillId
select new { b.SkillId, c.SkillName }).ToList();
lstboxSkill.DataSource = skill;
lstboxSkill.DataTextField = "SkillName";
lstboxSkill.DataValueField = "SkillId";
lstboxSkill.DataBind();
}

first list box is binding properly.
and i have written code for transfer selected item from one listbox to another in a button click event like
protected void imgbtnMoveRightListBox_Click(object sender, ImageClickEventArgs e)
{
for (int i = 0; i < lstboxSkill.Items.Count; i++)
{
if (lstboxSkill.Items[i].Selected)
{
lstBBoxSkill2.Items.Add(lstboxSkill.Items[i]);
}

}
}
Try this..

C#
foreach (ListItem item in lstLeft.Items)
{
  if (item.Selected)
  {
    lstRight.Items.Add(new ListItem() { Text = item.Text, Value = item.Value });
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900