寫個項目,從數據庫中獲得的數據綁定值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控件默認選中,提交時永遠獲得選中狀態的實現代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。