English 中文(简体)
Asp.net server side Conditional attribute or unchecked attribute?
原标题:

I have the following aspx code:

  <% foreach (ModelDefect modelDefect in GetCodes())
  {%>
<tr>
    <td><input disabled="disabled" name="<%= modelDefect.Code %>" value="<%= modelDefect.Code %>" type="checkbox" checked="<%=modelDefect.CompletedDate.HasValue? "checked":string.Empty %>" /></td>
    <td><a href="javascript:submitForm( <%= modelDefect.DefectId %> );"><%= modelDefect.Code %></a></td>   
    <td><%= modelDefect.Description %></td><td><%= modelDefect.AddedDate %></td>
</tr>
<% } %>

I want the check box to be checked if there is a completed date, and unchecked if not. W3 doesn t appear to say how to leave it unchecked but include the attribute. I don t see a way to conditionally include that attribute on the item. I found a page that suggests that checked="yes" or checked="no" should work but it hasn t. I d like a browser-independent and standards based solution or... a clean way to conditionally add that tag server side on my asp that is Ajax friendly? (Response.Write doesn t work in Asp.net AJax as I understand it.)

最佳回答

Write a helper method which can be unit tested and reused:

public static string GetCheckedAttribute(DateTime? value)
{
    return value.HasValue ? "checked="checked"" : string.Empty;
}

and call it in your page:

<input disabled="disabled" 
       name="<%= modelDefect.Code %>" 
       value="<%= modelDefect.Code %>" 
       type="checkbox" 
       <%= GetCheckedAttribute(modelDefect.CompletedDate) %>
/>
问题回答

Take the attribute and put it as a part of the conditional output, this should work for you

 <input disabled="disabled" 
        name="<%= modelDefect.Code %>" 
        value="<%= modelDefect.Code %>" 
        type="checkbox" 
        <%=modelDefect.CompletedDate.HasValue? "checked=checked":string.Empty %>"
  />




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签