Josiane Ferice
Josiane Ferice

Reputation: 941

CheckBox inside Telerik Grid is always Check Even when Value if false

I am using asp.net MVC 3 Telerik Extension. I have a checkbox inside a telerik grid that is supposed to be checked or unchecked. I noticed that the checkbox value were coming up correctly, they were either true or false. However, the checkboxes are still checked even when checked = "False".

below is a sample code:

columns.Bound(o => o.currentStudent).Title("Current Student")
  .Template(
   @<text>
      <input type="checkbox" id="chkCurrentStudent" name="chkCurrentStudent" value="@item.currentStudent" checked="@item.currentStudent" />
   </text>
  )

The HTML code that is render on the client side:

<input name="chkCurrentStudent" id="chkCurrentStudent" type="checkbox" checked="True" value="True">

What am I missing here?

Upvotes: 0

Views: 828

Answers (3)

Josiane Ferice
Josiane Ferice

Reputation: 941

After reading some of the answers, I changed the code. Here is how the latest code look now.

columns.Bound(o => o.currentStudent).Title("Current Student")
  .Template(
   @<text>
      <input type="checkbox" id="chkCurrentStudent" name="chkCurrentStudent" value="@item.currentStudent" @(item.currentStudent=="True" ? "checked=checked": "") />
   </text>
  )

Upvotes: 0

Nitin Varpe
Nitin Varpe

Reputation: 10694

Change

columns.Bound(o => o.currentStudent).Title("Current Student")
  .Template(
   @<text>
      <input type="checkbox" id="chkCurrentStudent" name="chkCurrentStudent" value="@item.currentStudent" checked="@item.currentStudent" />
   </text>
  )

to

  columns.Bound(o => o.currentStudent).Title("Current Student")
      .Template(
       @<text>
          <input type="checkbox" id="chkCurrentStudent" name="chkCurrentStudent" value="@item.currentStudent" 
@(item.currentStudent ? Html.Raw(" checked=\"checked\"") : null) />
       </text>
      )

Upvotes: 1

Craig
Craig

Reputation: 218

I believe the correct attributes for checkboxes in HTML is:

For checked boxes: checked="checked"

For unchecked boxes, you simply omit the checked="checked"

Because you are passing checked="false", your browser is still interpreting the checkbox as checked. It simply looks for the presence of the attribute checked when parsing the HTML. It doesn't matter what the value of the attribute is...

I added an example at jsfiddle: http://jsfiddle.net/pqz28/

Hope that helps!

Upvotes: 1

Related Questions