Skip to content

Latest commit

 

History

History
47 lines (30 loc) · 3.17 KB

creating-mutually-exclusive-checkboxes-cs.md

File metadata and controls

47 lines (30 loc) · 3.17 KB
uid title author description ms.author ms.date ms.assetid msc.legacyurl msc.type
web-forms/overview/ajax-control-toolkit/mutuallyexclusivecheckbox/creating-mutually-exclusive-checkboxes-cs
Creating Mutually Exclusive Checkboxes (C#) | Microsoft Docs
wenz
When only one of a set of options may be selected, radio buttons are usually used. There is a drawback, though: Once one radio button in a group is selected,... (C#)
riande
06/02/2008
8e11b813-ba0d-4c29-b0f8-f65db6dbef1e
/web-forms/overview/ajax-control-toolkit/mutuallyexclusivecheckbox/creating-mutually-exclusive-checkboxes-cs
authoredcontent

Creating Mutually Exclusive Checkboxes (C#)

by Christian Wenz

Download PDF

When only one of a set of options may be selected, radio buttons are usually used. There is a drawback, though: Once one radio button in a group is selected, it is not possible to uncheck all radio buttons. Check boxes can be unchecked at any time, however are not mutually exclusive. This tutorial provides the best of both approaches: check boxes that are mutually exclusive.

Overview

When only one of a set of options may be selected, radio buttons are usually used. There is a drawback, though: Once one radio button in a group is selected, it is not possible to uncheck all radio buttons. Check boxes can be unchecked at any time, however are not mutually exclusive. This tutorial provides the best of both approaches: check boxes that are mutually exclusive.

Steps

The ASP.NET AJAX Control Toolkit contains the MutuallyExclusiveCheckBox extender. This enables programmers to assign any checkbox to a group name (Key attribute). From all check boxes within the same group, only one may be selected at one time.

Let's start with putting two check boxes on a new ASP.NET page. There can be more, but two of them suffice to demonstrate the principle:

[!code-aspxMain]

For both checkboxes, a MutuallyExclusiveCheckBoxExtender control must be put on the page. Both Key attributes need to have the same value, just as the value attributes of HTML radio button elements must be identical to denote the group they belong to. The TargetControlID property of the extender points to the ID of the check box.

[!code-aspxMain]

Finally, include the ASP.NET AJAX ScriptManager which is required by all elements of the ASP.NET AJAX Control Toolkit:

[!code-aspxMain]

Save and run the page: You can check and uncheck both check boxes, however at no time can both check boxes be checked.

Only one checkbox can be checked at a time

Only one checkbox can be checked at a time (Click to view full-size image)

[!div class="step-by-step"] Next