uncheckable RadioButtons vs exclusive Checkboxes - asp.net

From a UI prospective, is it better to have a set of RadioButtons with the added functionality of being able to uncheck, or have a set of exclusive CheckBoxes, meaning only one can be checked at a time?
Update:
I did not expect such negative responses to this. Maybe it would help if I gave an example that is closer to how it's being used.
I have a GridView full of databound stuff. The user has the option of choosing one of the rows as "primary", but it's not required. new example:
$(":radio").click(function() {
if (this.previous) {
this.checked = false;
}
this.previous = this.checked;
});
$(":checkbox").click(function() {
$(":checkbox").not(this).prop("checked", false);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Choose a primary city and state (if applicable).<br />
<table><tr><td>
<table border="1" >
<tr><td>Primary</td><td>City</td><td>State</td></tr>
<tr><td><input type="radio" name="radio" /></td><td>Pahokee</td><td>Flordia</td></tr>
<tr><td><input type="radio" name="radio" /></td><td>Palatka</td><td>Flordia</td></tr>
<tr><td><input type="radio" name="radio" /></td><td>Palm Bay</td><td>Flordia</td></tr>
<tr><td><input type="radio" name="radio" /></td><td>Palm Beach Gardens</td><td>Flordia</td></tr></table></td><td> </td><td><table border="1" >
<tr><td>Primary</td><td>City</td><td>State</td></tr>
<tr><td><input type="checkbox" /></td><td>Pahokee</td><td>Flordia</td></tr>
<tr><td><input type="checkbox" /></td><td>Palatka</td><td>Flordia</td></tr>
<tr><td><input type="checkbox" /></td><td>Palm Bay</td><td>Flordia</td></tr>
<tr><td><input type="checkbox" /></td><td>Palm Beach Gardens</td><td>Flordia</td></tr>
</table></td><tr>
</table>
Should I include an extra control for unchecking the "primary", or just extend the functionality of the CheckBox or RadioButton ?
If you think extra RadioButton, where would that go, in the header?
BTW, it looks like JavaScript is needed to make RadioButtons work in a GridView anyway because of ASP.Net munging the GroupName.
Update 2:
Also, see ASP.NET AJAX extender MutuallyExclusiveCheckBox

Definitely use radio buttons, as they are meant for this purpose. Why confuse the user with checkboxes and further trouble yourself by writing code to maintain exclusive behaviour?

Though I don't agree with changing the expected functionality of radio and checkbox controls, I have had cases where I needed to.
If you do this with Javascript, it's going to fail spectacularly if your user has JS disabled.
The appearance CSS attribute is your friend.

If the user can pick only one choice out of a set, use radiobuttons. If the user can pick any number of choices, use checkboxes. Note that the definition of "only one choice" can include a radiobutton that says "none".
That's been the standard on pretty much every platform since GUIs were invented. Deviating from that practice will only serve to confuse your users.

Like other people have said, you shouldn't change the expected behaviour of native form elements. I would use a group of radio buttons, and include a button for 'clear selection'. That makes it explicit that the selection is clearable, and provides an obvious way of doing it.
Another way to do it would be to 'invent' a new type of control - probably based on hidden radio buttons, perhaps something that obviously looked like a group of 'toggles'. This is a very visual solution though, and would rely on javascript, so it's probably not the most reliable choice.
Here's an example of both solutions:
http://www.spookandpuff.com/examples/clearableOptions.html
Both solutions are currently javascript reliant - you could easily give the first option a JS-free fallback by having the clear button give the form to the server, and respond with a cleared radio button set.

Related

Request.Form is nothing when the object is disabled

I Google and search every article here and in the google but i cannot find anything regarding this issue. Any idea why the request.form["name here"] is nothing when the html control is disabled something like
<input type="radio" name="name here" disabled />
removing disabled would return the values. I tested this only on IE 10 not sure on other browsers.
I am looking into changing the css of the radio button but that is a different story.
Thank you.
Disabled form fields are not part of the posted data. If you disable the html control the data will not be posted.
If you want to show the data and have it posted, but not possible to edit you can set the control to readonly instead.
<input type="radio" name="name here" readonly="readonly" />

Post form without a button

I was exploring the search box on the Apple website and noticed it doesn't have a input type="submit" to post the form, even when Javascript is disabled.
<form action="/search/" method="post" class="search" id="g-search">
<div class="sp-label">
<label for="sp-searchtext">Search</label>
<input type="text" name="q" id="sp-searchtext" accesskey="s">
</div>
</form>
Having never really explored it, I take it from this it means you can post a form without needing a submit button, it just relies on the user pressing the return key.
Two questions: 1) Is this compatible across all browsers? So in IE 7 will pressing return still work?; 2) Is there a way to do this in ASP.NET without using an asp:button? I will probably have it inside a placeholder (where I would conventionally use defaultButton to allow for multiple forms on the page) but if I can get rid of the button altogether then that's a plus.
yes of course it is possible to do it in anyway you want.
The simpler thing is to have an onclick event that calls a function that does the submit like this:
JQuery:
$('#id_of_form').submit()
javascript:
document.name_of_my_form.submit();
or
document.getElementById('id_of_my_form').submit();
so simple :)

show html checkbox as neither checked or non-checked

I have an html checkbox that controls a list of checkboxes. I want this checkbox to display in a sorta "null" state where it is neither true nor false. Is this possible?
<HeaderTemplate>
<div style="width:90px">
Toggle:
<input id="chkAll"
onclick="javascript:SelectAllCheckboxes(this);"
runat="server" type="checkbox" />
</div>
</HeaderTemplate>
No, a checkbox won't allow a custom third state. You need to find another way to handle it. A few come to my mind:
Use a dropdown list with three values, or three radio buttons
Use two checkboxes, one for assigned/null, second for checked/unchecked
Use an image and javascript to fill a hidden numeric field (it could be a fake checkbox, but it will not match each browser's look and feel and could look weird)
HTML doesn't natively support the notion of a three-state checkbox. You'd have to implement it with a custom control, using a combination of images and text.
no it isn't. but you can add additional property like you have 'runat'.
<input id="chkAll"
onclick="javascript:SelectAllCheckboxes(this);"
runat="server" type="checkbox" null="true" />
or you can add "disabled" instead of "checked"
here is js plugin that can help you:
http://www.blueshoes.org/en/javascript/checkbox/

how the exclude a asp.net web control from the tabbing order

I've been trying to exclude an asp.net web control from the tabbing order.
The control that i'm using is the RadioButtonList control. I've try setting the TabIndex to either 0 or -1.
The problem that i'm running into is ... initially the control is skipped (which is good), but it seemed like the engine just shifted the control to the end of the tabbing order.
Is this an expected behavior? or is there a work around for this?
After checking the HTML source, I have some interesting findings ...
<td><input id="rbSpiffType_0" type="radio" name="rbSpiffType" value="R" checked="checked" tabindex="-1" /><label for="rbSpiffType_0">Regular Spiff</label></td><td>
I think i might be tabbing into the label ... any ideas how to fix that in ASP.NET?
The issue may be the individual radio buttons don't have a tab index of -1, try looping through the RBL's Items collection, and do:
item.Attributes.Add("tabindex", "-1");
And see if that clears it up. It might actually be item.Attributes["tabindex"], can't remember the exact syntax at the moment.

Radiobutton list auto postback

Ok, this is painful for me because yes, I've been coding ASP.NET after classic ASP days. I've so lost the ways of regular HTML controls.
So, here's my scenario.
1) I have a list of radiobuttons on the page, regular old HTML buttons:
<div id="selectOptions">
<form action="Checkout.aspx" method="post" target="_self">
<ul>
<li><label><input type="radio" name="rbRewardsSelectionGroup" id="rbtNoRewardOption" checked="checked" value="0" />None</label></li>
<li><label><input type="radio" name="rbRewardsSelectionGroup" value='1' />Free Shipping</label></li>
<li><label><input type="radio" name="rbRewardsSelectionGroup" value='2' />$100 Store Credit</label></li>
<li><label><input type="radio" name="rbRewardsSelectionGroup" value='3' />$250 Store Credit</label></li>
<li><label><input type="radio" name="rbRewardsSelectionGroup" value='4' />$500 Stored Credit</label></li>
</ul>
</form>
</div>
2) I need to add some javascript to force a postback if any one of these radiobuttons are selected, even the default selected radio button
3) I attempted to add a form and wrap it around the code. I don't know if this will work because I think ASP.NET won't let you have more than one form on a page...or maybe that's just how it is for everyone?
4) Lets say it will work. Ok so I click the radio button. I send a request to Checkout.aspx and then in the code-behind I can grab that data and do a Request["rbRewardsSelectionGroup"] to get reference to that radiobutton group and then perform my server-side value checks...whatever I need to do.
I guess my question is am I going about this right with the way I have this setup currently? I believe I've got the overall concept here outside of using ASP.NET based controls that do all this magic for you.
I don't want to get into why I'm not using an ASP.NET control for this particular piece of code.
2) With jquery, listen for the change event on the radio buttons and submit its parent form:
$("input[#name='rbRewardsSelectionGroup']").change(function(){
$(this).parents('form').submit();
});
3) You can only have one form with a runat="server" property in asp.net
4) Yes, use Request.Form["rbRewardsSelectiongroup"] to get the value of the selection.

Resources