我在 asp GridView 的模板字段中有一个按钮和一个复选框。我希望根据数据字段到期时间有条件地设置按钮的禁用属性和复选框的选中属性。如果“过期”等于“永久”,则应禁用该按钮并选中该复选框。如果没有,则启用按钮并取消选中复选框。我试过了:
<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" %>'/>
Run Code Online (Sandbox Code Playgroud)
但似乎仅仅列出禁用和检查的属性就会导致所有按钮被禁用并检查所有复选框。
disabled和checked是布尔属性。他们分别只接受值disabled和checked。
如果您不希望默认情况下选中/禁用该控件,则根本不要包含该属性。
由于它们是布尔属性,因此您可以省略除值之外的所有内容。
<input type="button"
id="expiration"
<%# (string)Eval("Expiration") == "Permanant" ? "disabled" : "" %>
value='<%# Eval("Expiration") %>'>
<input type="checkbox"
id="permanent"
<%# (string)Eval("Expiration") == "Permanant" ? "checked" : "" %>>
Run Code Online (Sandbox Code Playgroud)
不过,您的复选框应该有一个明确的值,请参阅 DTD 中的注释:
value CDATA #IMPLIED -- Specify for radio buttons and checkboxes --
Run Code Online (Sandbox Code Playgroud)