Dotnet 3.5 interview questions and answers Headline Animator

Tuesday, September 8, 2009

How to use checkbox

Always use Html.CheckBox() overload methods for creating checkbox controls.

Notice that the check box helper (Html.CheckBox()) renders two input controls. First, it renders a check box control as you’d expect, and then it renders a hidden input control of the same name. This is to work around the fact that when check boxes are deselected, browsers don’t submit any value for them. Having the hidden input control means the MVC Framework will receive the hidden field’s value (i.e., false) when the check box is unchecked.
Checkbox doesn't have a label. So add the html label tag with for attribute with value as “name of checkbox”

Code:




<%= Html.CheckBox("chkAccept2", true )%>
<label for="chkAccept2">Accept Terms & Conditions</label>
<input name="chkAccept2" type="hidden" value="false" />
<label for="chkAccept2">Accept Terms & Conditions</label>
<%= Html.CheckBox("chkAccept3", false, new { @class="error", @client_selector="alphanumeric" }) %>
<label for="chkAccept3">Accept Terms & Conditions</label>
<input class="error" client_selector="alphanumeric" id="chkAccept3" name="chkAccept3" type="checkbox" value="true" />
<input name="chkAccept3" type="hidden" value="false" /><label for="chkAccept3">Accept Terms & Conditions</label>

Source : Apress.Pro.ASP.NET.MVC.Framework.

No comments:

Post a Comment