The implementation code for the CheckBox control that is selected by default and always gets the selected state when it is submitted

  • 2021-07-26 07:24:26
  • OfStack

Write a project, the data binding value Checkbox obtained from the database, and the binding method is as follows


// Overtime setting data binding 
protected void CheckBoxBind()
  {
    OverTimeBLL overTimeBll = new OverTimeBLL();
    List<OverTime> overTimeList = new List<OverTime>();

    overTimeList = overTimeBll.GetAll();
    if (overTimeList.Count > 0)
    {
      // Bind page information 
      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();
    }
  }

Then add CheckBoxBind () method to protected void Page_Load (object sender, EventArgs e) method, but when submitting, if a certain CheckBox is selected, it will always be that the checked attribute of the checkBox is True. Later, I tried it again and found that I was negligent, so long as I put the CheckBoxBind method in if (! this. Page. IsPostBack).

The code is as follows


if (!this.Page.IsPostBack)
      {
        remindResult.Text = GetRemind();
        // Page data binding 
        CheckBoxBind();
      }

Related articles: