设置Eval(bool)的选中值

我有一个房产

public bool AutoRenew { get; set; } 

在页面中:

 <input type="checkbox" checked='' /> 

但即使属性的值为false ,也始终检查它。

我尝试了以下变化:

 <input type="checkbox" checked='' /> <input type="checkbox" checked='' /> <input type="checkbox" checked='' /> 

但没有任何作用,它一直在检查。 表达式应该是什么样的?

编辑:这是页面中有问题的部分:

 ...   
<input type="checkbox" checked='' /> ...

您正在使用普通的html复选框

要将数据绑定到palin html复选框,您必须使用checked =“checked”

如果您使用ASP.NET Checkbox控件,那么您的原始代码将顺利运行。

绑定数据时,普通html控件和ASP.NET控件之间存在差异。

  //for asp.net checkbox " /> //for plain html checkbox  /> 

期望的输出HTML应该让你顺利:

   

这意味着,为了不选中复选框,您根本不应该在输出中提及checked属性,甚至不应该使用false值。

如果Convert.ToBoolean(Eval("AutoRenew"))true则添加checked属性

  /> 

你可以检查Grid_RowDataBound事件中的任何类型值:

aspx:

         

aspx.cs:

 protected void GridMain_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //find the checkboxes in the template field. CheckBox grid_chkbox= (CheckBox)e.Row.FindControl("grid_chkbox"); //find boolean value in current record grid_chkbox.Checked = e.Row.DataItem.boolvalue; } }