Conditionally set Disabled and Checked attributes on HTML Elements
asp.net, html
Solution
`disabled` and `checked` are boolean attributes. They accept only the values `disabled` and `checked` respectively.
If you don't want the control to be checked/disabled by default then do not include the attribute at all.
Since they are boolean attributes, you may omit everything except the value.
<input type="button"
id="expiration"
<%# (string)Eval("Expiration") == "Permanant" ? "disabled" : "" %>
value='<%# Eval("Expiration") %>'>
<input type="checkbox"
id="permanent"
<%# (string)Eval("Expiration") == "Permanant" ? "checked" : "" %>>
Your checkbox should have an explicit value though, see the comment in the DTD:
value CDATA #IMPLIED -- Specify for radio buttons and checkboxes --
Problem
I have a button and a checkbox in a template field in an asp GridView. I want the disabled property of the button and the checked property of the checkbox to be set conditionally based on the Data Field Expiration. If Expiration equals Permanent, then the button should be disabled and the checkbox checked. If not, then the button is enabled and checkbox unchecked. I've tried: ``` <input type="button" id="expiration" disabled='<%# (string)Eval("Expiration") == "Permanant" ? "disabled" : "enabled" %>' value='<%# Eval("Expiration") %>'/> <input type="checkbox" id="permanent" checked= '<%# (string)Eval("Expiration") == "Permanant" ? "checked" : "unchecked" %>'/> ``` But it seems that just having the disabled and checked attributes listed at all is causing all buttons to be disabled and all checkboxes checked.