CheckBoxList数据绑定及取值(转)
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(this.IsPostBack)
return;
this.BindTheList();
}
private void BindTheList()
{
this.CheckBoxList1.DataSource=this.getDataTable();
this.CheckBoxList1.DataTextField="id";
this.CheckBoxList1.DataValueField="name";
this.CheckBoxList1.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
int num=this.CheckBoxList1.Items.Count;
string str=null;
for(int i=0;i
{
if(this.CheckBoxList1.Items[i].Selected==true)
{
str +=CheckBoxList1.Items[i].Value.ToString();
// Response.Write(this.CheckBoxList1.SelectedItem.Value);
}
}
Response.Write(str);
}
CheckBoxList取值
string strrighgs = "";
for (int i = 0; i < this.cblrighgs.Items.Count; i++)
{
if (this.cblrighgs.Items[i].Selected == true)
{
strrighgs += this.cblrighgs.Items[i].Value+"|";
}
}
string str = "";
foreach (ListItem li in cblrighgs.Items)
{
if (li.Selected == true)
{
str += li.Value+";";
}
}
Response.Write(str);
Response.End();
CheckBoxList取值及勾选
string[] strtemp = strapp.Split('|');
foreach (string str in strtemp)
{
for (int i = 0; i < cblrighgs.Items.Count; i++)
{
if (this.cblrighgs.Items[i].Value == str)
{
this.cblrighgs.Items[i].Selected = true;
}
}
}
string strTerm=rdr["department"].ToString();
string[] strTerms = strTerm.Split(';');
foreach(ListItem lItem in this.CheckBoxList.Items)
{
for (int i = 0; i < strTerms.Length; i++)
{
if (lItem.Value == strTerms[i])
{
lItem.Selected = true;
break;
}
}
}