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;
                        }
                    }
                }

出处http://blog.sina.com.cn/s/blog_a35224ff01011wek.html

posted on 2014-01-03 11:14  不小心而已  阅读(1529)  评论(0编辑  收藏  举报