listbox怎么取得所有选择了的项的值,我是多选项的,,但我最后在数据库里选项的只有第一个选项的值,,,应该怎么写??

解决方案 »

  1.   

    listAccount为listbox
    string str="";
    for(int i=1;i<listAccount.Items.Count;i++)
    {
    if(listAccount.Items[i].Selected)
    {
    str+=listAccount.Items[i].Value+',';//逗号为分割所用
    }
    }
    if(str.Length>0)
    {
    str=str.Substring(0,str.Length-1);//去掉最后一个逗号
    }
    就可以得到所选中的值了
      

  2.   

    foreach(ListItem temp in ListBox1.Items)
        if(temp.Selected)
              str+=temp.Text;最后得到的str即为所有的选择文本,如果要值,可将temp.Text改为Value。说明:可能会有大小写之误,自己判别。