欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CheckBox控件默认选中,提交时永远获得选中状态的实现代码

程序员文章站 2023-12-09 19:28:57
写个项目,从数据库中获得的数据绑定值checkbox,绑定方法如下 //加班设置数据绑定 protected void checkboxbind()...

写个项目,从数据库中获得的数据绑定值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控件默认选中,提交时永远获得选中状态的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。