checkboxlist选中取不到值的问题解决
模拟情景:
protected void Page_Load(object sender, EventArgs e) { Maticsoft.BLL.t_zhuanye bll = new Maticsoft.BLL.t_zhuanye(); List<Maticsoft.Model.t_zhuanye> listZhuanye = bll.GetModelList(""); if (listZhuanye.Count != 0) { CheckBoxList1.DataSource = listZhuanye; CheckBoxList1.DataTextField = "name1"; CheckBoxList1.DataValueField = "ID"; CheckBoxList1.DataBind(); } if (!IsPostBack) { } } protected void SaveChanges_Click(object sender, EventArgs e) { string selval = ""; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { selval += CheckBoxList1.Items[i].Value + ","; } } selval = (string.IsNullOrEmpty(selval)) != true ? selval.Remove(selval.Length - 1) : "''"; }
问题:
在
SaveChanges_Click中,
selval一直取不到值!!!
解决办法:
把对
CheckBoxList1的绑定,移动到
if (!IsPostBack) {}内,问题解决!!
参考文章
1.绑定CheckBoxList:http://heisetoufa.iteye.com/blog/293193
2.绑定中取不到值:http://topic.csdn.net/u/20090426/15/4b8258c8-fe3b-45ec-a87d-e828eadfe6b3.html