下面小編就為大家?guī)硪黄狢heckBox控件默認(rèn)選中,提交時永遠(yuǎn)獲得選中狀態(tài)的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。
寫個項目,從數(shù)據(jù)庫中獲得的數(shù)據(jù)綁定值Checkbox,綁定方法如下:
//加班設(shè)置數(shù)據(jù)綁定
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是選中狀態(tài),那獲得的永遠(yuǎn)是該checkBox的checked屬性為True,后來再三試了,發(fā)現(xiàn)自己疏忽了,只要將CheckBoxBind方法放在if (!this.Page.IsPostBack)下即可。
代碼如下:
if (!this.Page.IsPostBack)
{
remindResult.Text = GetRemind();
//頁面數(shù)據(jù)綁定
CheckBoxBind();
}
以上這篇CheckBox控件默認(rèn)選中,提交時永遠(yuǎn)獲得選中狀態(tài)的實現(xiàn)代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考