Selection Controls in ASP.NET

Introduction and Demonstration

Selection controls allow the user to select one or more values from a list. They include both the CheckBox and RadioButton controls, which are designed to work in a group. The RadioButton control allows you to select only one option out of the group, whereas the CheckBox control allows you to select zero or more options.

ControlPurpose
CheckBoxSelects or unselects an option. You can toggle the selection.
RadioButtonSelects only one option out of a group. You can unselect an option only by selecting another RadioButton control in the group.
ListBoxAllows the user to select one or more options from a list represented by ListItem controls. This control always occupies a fixed space in the form.
DropDownListAllows the user to select only one option out of a list represented by ListItem controls. This control is used where the space in the form is limited.
RadioButtonListPresents a list of radio buttons represented by ListItem controls and allows selection of only one option.
CheckBoxListPresents a list of checkboxes represented by ListItem controls and allows you to select zero or more of the options.

The simplified syntax of the selection controls is as follows:

<asp:checkbox id="MyCheckBox1" text="Vanilla" runat="server" />
<asp:checkbox id="MyCheckBox2" text="Chocolate" runat="server" />

<asp:radiobutton id="MyRadioButton1" groupname="Group1" checked="True" text="Yes"
    runat="Server"
/>
<asp:radiobutton id="MyRadioButton2" groupname="Group1" text="No" runat="Server" />

<asp:listbox id="MyListBox" runat="server">
<asp:listitem value="Vanilla" selected="true">Vanilla</asp:listitem>
<asp:listitem value="Chocolate">Chocolate</asp:listitem>
<asp:listitem value="Strawberry">Strawberry</asp:listitem>
</asp:listbox>

<
asp:dropdownlist id="MyDropDownList" runat="server">
<asp:listitem value="Single" selected="true">Single</asp:listitem>
<asp:listitem value="Multiline">Multiline</asp:listitem>
<asp:listitem value="Password">Password</asp:listitem>
</asp:dropdownlist>

<
asp:checkboxlist id="MyCheckBoxList" repeatdirection="vertical" runat="server">
<asp:listitem value="Vanilla" text="Vanilla"/>
<asp:listitem value="Chocolate" text="Chocolate"/>
<asp:listitem value="Strawberry" text="Strawberry"/>
</asp:checkboxlist>

<
asp:radiobuttonlist id="MyRadioButtonList" repeatdirection="Horizontal" runat="server">
<asp:listitem value="Female" text="Female" selected="true"/>
<asp:listitem value="Male" text="Male"/>
</asp:radiobuttonlist>

The controls can then be referenced programmatically with code fragments like the following:

MyCheckBox1.Checked = True
MyRadioButton1.Checked = False
MyListBox.SelectionMode = ListSelectionMode.Multiple
MyDropDownList.SelectedIndex = 1
MyCheckBoxList.RepeatDirection = RepeatDirection.Horizontal
MyRadioButtonList.RepeatLayout = RepeatLayout.Table


Index.aspx Page

<%@ Page Language="vb" %> 
<html>
<
head>
    <title>Selection Control Example</title>
    <script runat="server">
Sub Page_Load( )
MyCheckBox1.Checked = True
MyRadioButton1.Checked = False
MyListBox.SelectionMode = ListSelectionMode.Multiple
MyDropDownList.SelectedIndex = 1
MyCheckBoxList.RepeatDirection = RepeatDirection.Horizontal
MyRadioButtonList.RepeatLayout = RepeatLayout.Table
End Sub
    </script
>
</head>
<
body>
    <h1>
        Selection Control Example</h1>
    <form id="Form1" runat="server">
    <asp:Table ID="MyTable" border="1" CellPadding="5" CellSpacing="0" runat="server">
        <asp:TableRow ID="Tablerow1" runat="server">
            <asp:TableCell ID="Tablecell1" runat="server">
CheckBox Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell2" runat="server">
                <asp:CheckBox ID="MyCheckBox1" Text="Vanilla" runat="server" />
                <asp:CheckBox ID="MyCheckBox2" Text="Chocolate" runat="server" />
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow2" runat="server">
            <asp:TableCell ID="Tablecell3" runat="server">
RadioButton Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell4" runat="server">
                <asp:RadioButton ID="MyRadioButton1" GroupName="Group1" Checked="True" Text="Yes"
                    runat="Server" />
                <asp:RadioButton ID="MyRadioButton2" GroupName="Group1" Text="No" runat="Server" />
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow3" runat="server">
            <asp:TableCell ID="Tablecell5" runat="server">
ListBox Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell6" runat="server">
                <asp:ListBox ID="MyListBox" runat="server">
                    <asp:ListItem Value="Vanilla" Selected="true">Vanilla</asp:ListItem>
                    <asp:ListItem Value="Chocolate">Chocolate
                    </asp:ListItem
>
                    <asp:ListItem Value="Strawberry">Strawberry
                    </asp:ListItem
>
                </asp:ListBox>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow4" runat="server">
            <asp:TableCell ID="Tablecell7" runat="server">
DropDownList Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell8" runat="server">
                <asp:DropDownList ID="MyDropDownList" runat="server">
                    <asp:ListItem Value="Single" Selected="true">Single</asp:ListItem>
                    <asp:ListItem Value="Multiline">Multiline
                    </asp:ListItem
>
                    <asp:ListItem Value="Password">Password</asp:ListItem>
                </asp:DropDownList>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow5" runat="server">
            <asp:TableCell ID="Tablecell9" runat="server">
CheckBoxList Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell10" runat="server">
                <asp:CheckBoxList ID="MyCheckBoxList" RepeatDirection="vertical" runat="server">
                    <asp:ListItem Value="Vanilla" Text="Vanilla" />
                    <asp:ListItem Value="Chocolate" Text="Chocolate" />
                    <asp:ListItem Value="Strawberry" Text="Strawberry" />
                </asp:CheckBoxList>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow6" runat="server">
            <asp:TableCell ID="Tablecell11" runat="server">
RadioButtonList Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell12" runat="server">
                <asp:RadioButtonList ID="MyRadioButtonList" RepeatDirection="Horizontal" runat="server">
                    <asp:ListItem Value="Female" Text="Female" Selected="true" />
                    <asp:ListItem Value="Male" Text="Male" />
                </asp:RadioButtonList>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    </form>
</body>
</
html>

HAVE A HAPPY CODING!


Similar Articles