取消选中后,复选框始终返回true

时间:2014-03-07 07:18:12

标签: c# asp.net gridview checkbox

我已嵌套Gridview,而最后Gridview已嵌套Checkbox

检查Checkbox是否已选中或未选中<

的条件

foreach (GridViewRow gvr in GridView1.Rows)
{
       GridView GridView2= gvr.FindControl("GridView2") as GridView;
       foreach (GridViewRow gvr2 in GridView2.Rows)
       {
           GridView GridView3= gvr2.FindControl("GridView3") as GridView;
           foreach (GridViewRow gvr3 in GridView3.Rows)
           {
               if(((CheckBox)gvr3.FindControl("chk1")).Checked)
               {
                    string txt = txtKeyboard.text;
               }
           }
       }
}

即使未经检查,它也会通过条件

2 个答案:

答案 0 :(得分:1)

if(((CheckBox)gvr3.FindControl("chk1")).Checked)
{
     string txt = txtKeyboard.text;
}

而不是上面的代码只需尝试一次

  CheckBox chk1 = gvr3.Cells[0].Controls[0] as CheckBox;
  if(chk1.checked==true)
  {
      string txt = txtKeyboard.text;
  }

这里你需要设置单元格值和控制值,而不是0

答案 1 :(得分:0)

如何将数据绑定到“GridView2”控件?如果你调用了一些类似“GridView2_DataBind”的方法,在处理完回发数据之后,girdview的状态将被重置。

例如,

 protected void Page_Load(object sender, EventArgs e)
 {
          if (!this.IsPostBack){ 
              GridView2_DataBind() // you shouldn't call databind method when postback
          }
 }