如何从asp.net中的checkboxlist控件中获取所有选定的项目? [英] how to get all selected items from checkboxlist control in asp.net?

查看:255
本文介绍了如何从asp.net中的checkboxlist控件中获取所有选定的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

clsbo objbo = new clsbo();

foreach(CheckBoxList1.Items中的ListItem li)

{

if(li.Selected) )

{

objbo.Hobbies = CheckBoxList1.SelectedValue.ToString();

}



}



}



这里我只获得第一个选中的值checkboxcontrol但我只想要

来获取checkboxlistcontrol中的所有值。

clsbo objbo = new clsbo();
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
objbo.Hobbies = CheckBoxList1.SelectedValue.ToString();
}

}

}

here i am only getting the first selected value from the checkboxcontrol but i just want
to get all the values from the checkboxlistcontrol.

推荐答案

string result =;



foreach(ListItem li in this.Checckboxlist1.Items)

{

if(li.selected == True)

{

string tu = li.value;

result = tu +,+ result;

}

}
string result="";

foreach(ListItem li in this.Checckboxlist1.Items)
{
if (li.selected==True)
{
string tu=li.value;
result=tu+","+result;
}
}


试试..



Try..

class clsbo
{
    public List<string> hobbies{ get; set; }
}







clsbo objbo= new clsbo();
objbo.hobbies = new List<string>();
foreach (ListItem li in CheckBoxList1.Items)
{
  if(li.Selected)
  {
    if (li.Value != null && li.Value != "")
    {
        objbo.hobbies.Add(li.Value);
    }
   }
}


这篇关于如何从asp.net中的checkboxlist控件中获取所有选定的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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