Setting checked value for Eval(bool)

故事扮演 提交于 2019-12-21 09:28:16

问题


I have a property

public bool AutoRenew
{
    get;
    set;
}

And in the page:

<input type="checkbox" checked='<%# Eval("AutoRenew") %>' />

but it is always checked, even if the value of the property is false.

I tried the following variations:

<input type="checkbox" checked='<%# Convert.ToBoolean(Eval("AutoRenew")) %>' />
<input type="checkbox" checked='<%# Convert.ToBoolean(Eval("AutoRenew")) == true %>' />
<input type="checkbox" checked='<%# (Boolean)Eval("AutoRenew") %>' />

but nothing works, it keeps being checked. What should the expression look like?

EDIT: Here is the problematic part in the page:

...
<asp:ListView ID="MyListView" runat="server">
    <LayoutTemplate>
        <table class="ms-listviewtable" style="background-color: White;">
            <tr class="ms-viewheadertr ms-vhltr">
                <th class="ms-vh-icon" scope="col">
                    <input type="checkbox" />
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Training Item</a></div>
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Training Task Type</a></div>
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Due Date</a></div>
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Auto-Renew</a></div>
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Training Reason</a></div>
                </th>
            </tr>
            <tr id="itemplaceholder" runat="server"></tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr class="ms-itmhover">
            <td class="ms-vb-itmcbx ms-vb-firstCell">
                <input type="checkbox" class="s4-itm-cbx" />
            </td>
            <td class="ms-vb-title">
                <div class="ms-vb itx"><a><%# Eval("Title")%></a></div>
            </td>
                <td class="ms-vb2">
                    <asp:DropDownList ID="TaskTypeDropDownList" runat="server">
                    </asp:DropDownList>
                </td>
                <td class="ms-vb2"><%# Eval("DueDate")%></td>
                <td class="ms-vb2" style="text-align: center;">
                    <input type="checkbox" checked='<%# Convert.ToBoolean(Eval("AutoRenew")) %>' />
                </td>
                <td class="ms-vb2"><%# Eval("TrainingReason")%></td>
            </tr>
        </ItemTemplate>
        ...

回答1:


You are using plain html checkbox

to bind data to palin html checkbox you must use checked="checked"

If you use ASP.NET Checkbox control then your original code will work smoothly.

There is difference between plain html controls & ASP.NET controls when binding data.

 //for asp.net checkbox
 <asp:CheckBox  ID="IdCheckBox" runat="server" Checked="<%# Convert.ToBoolean(Eval("AutoRenew")) %>"  />

//for plain html checkbox
<input type="checkbox" <%# Convert.ToBoolean(Eval("AutoRenew")) ? "checked" : "" %> />



回答2:


Desired output HTML should get you on the way:

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

This means that, to NOT check the checkbox, you should not mention the checked attribute in the output at all, not even with a value of false.




回答3:


Add checked attribute if Convert.ToBoolean(Eval("AutoRenew")) is true

<input type="checkbox" 
  <%# Convert.ToBoolean(Eval("AutoRenew")) ? "checked" : string.Empty %> /> 



回答4:


you can check anytype value in Grid_RowDataBound Event :

aspx :

<asp:GridView ID="GridMain" runat="server" OnRowDataBound="GridMain_RowDataBound">                   
     <Columns>                      
        <asp:TemplateField>
        <ItemTemplate>
                   <asp:CheckBox runat="server" ID="grid_chkbox" Enabled="false" />
                </ItemTemplate
        </asp:TemplateField>                          
     </Columns>              
</asp:GridView>

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;
   }
}



回答5:


I was facing issue with accepted answer. In case somebody is looking for getting asp checkbox checked property to work, here is code which worked for me :

<td><asp:CheckBox ID="chkHasAbility" runat="server"   Checked='<%#bool.Parse(Eval("HasAbility").ToString())%>' /> </td> 


来源:https://stackoverflow.com/questions/19182859/setting-checked-value-for-evalbool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!