如何保存多个Listbox值

时间:2013-12-14 20:42:32

标签: c# linq entity-framework listbox

从另一个列表框中选择值后,我使用linq to entity将多个列表框值保存到表的单个列中

protected void link1_Click(object sender, EventArgs e)
{
    if (lb1.SelectedItem != null)
    {
        lb2.Items.Add(new ListItem(lb1.SelectedItem.Text, lb1.SelectedItem.Value));
        lb1.Items.RemoveAt(lb1.SelectedIndex);
    }
}
protected void link2_Click(object sender, EventArgs e)
{
    if (lb2.SelectedItem != null)
    {
        lb1.Items.Add(new ListItem(lb2.SelectedItem.Text, lb2.SelectedItem.Value));
        lb2.Items.RemoveAt(lb2.SelectedIndex);
    }
}
protected void Button5_Click(object sender, EventArgs e)
{
    string Skill = lb2.SelectedItem.Text;

    employee e1 = new employee();
    e1.emp_skill = Skill;
    je.employee.AddObject(e1);
    je.SaveChanges();

    mv.ActiveViewIndex = 4;
}

1 个答案:

答案 0 :(得分:2)

你正在寻找这样的东西吗?我遍历列表框Id =“lb2”

的所有元素
protected void Button5_Click(object sender, EventArgs e)
{
        for(var i=0;i<lb2.Items.Count;i++)
        {
            var e1 = new employee() { emp_skill = lb2.Items[i].Text };
            je.employee.AddObject(e1);
        }
        je.SaveChanges();
}

如果您只想要列表框中的所选项目Id =“lb2”:

protected void Button5_Click(object sender, EventArgs e)
{
        ListItem item = null;
        for(var i=0;i<lb2.Items.Count;i++)
        {
            item = lb2.Items[i];
            if (item.Selected)
            {
                var e1 = new employee() { emp_skill = item.Text };
                je.employee.AddObject(e1);
            }
        }
        je.SaveChanges();
}