CheckBox控件默認(rèn)選中,提交時(shí)永遠(yuǎn)獲得選中狀態(tài)的實(shí)現(xiàn)代碼

字號(hào):


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