写个项目,从数据库中获得的数据绑定值Checkbox,绑定方法如下
//加班设置数据绑定 protected void CheckBoxBind() { OverTimeBLL overTimeBll = new OverTimeBLL(); List<OverTime> overTimeList = new List<OverTime>(); overTimeList = overTimeBll.GetAll(); if (overTimeList.Count > 0) { //绑定页面信息 txtID.Text = overTimeList[0].ID.ToString(); if (overTimeList[0].IsEarlyValid.ToLower() == "true") cbIsEarlyValid.Checked = true; if (overTimeList[0].IsLaterValid.ToLower() == "true") cbIsLaterValid.Checked = true; if (overTimeList[0].IsOnlyHoliday.ToLower() == "true") cbIsOnlyHoliday.Checked = true; if (overTimeList[0].IsUseTime.ToLower() == "true") cbIsUseTime.Checked = true; if (overTimeList[0].IsUseNum.ToLower() == "true") cbIsUseNum.Checked = true; txtMinDuration.Text = overTimeList[0].MinDuration.ToString(); } }
然后在protected void Page_Load(object sender, EventArgs e)方法中加入CheckBoxBind()方法,但提交时,如果某一个CheckBox是选中状态,那获得的永远是该checkBox的checked属性为True,后来再三试了,发现自己疏忽了,只要将CheckBoxBind方法放在if (!this.Page.IsPostBack)下即可。
代码如下
if (!this.Page.IsPostBack) { remindResult.Text = GetRemind(); //页面数据绑定 CheckBoxBind(); }
以上这篇CheckBox控件默认选中,提交时永远获得选中状态的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。