7.14. Adding a Group of RadioButton Controls

The RadioButton of Web Forms is similar to that of Windows Forms, except that by default the event is cached rather than immediately handled. In the following example I've used a RadioButtonList control instead, binding to its DataSource property in a manner similar to that of DropDownList:

private void Page_Load( object sender, System.EventArgs e )
{
    if ( ! IsPostBack )
    {
         string [] trainers = {
                  "Jennifer Juniper",
                  "Brian Bradley",
                  "Anita Anastasia"
          };

          RadioButtonList1.DataSource = trainers;
          RadioButtonList1.DataBind();
    }
}

SelectedIndexChanged() is also the event handler for this control. The SelectedIndex property identifies the selected radio button by its position within the item list. SelectedItem is a handle to the actual item. The implementation looks the same:

private void
RadioButtonList1_SelectedIndexChanged( object s, EventArgs e )
     { Label4.Text = RadioButtonList1.SelectedItem.Text; }

We can control the layout of the radio buttons through the RepeatColumns and RepeatDirection properties of the control. For example, rather than displaying the buttons from top to bottom, we can display them horizontally in three columns as follows:

RadioButtonList1.RepeatColumns = 3;
RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;

The radio button items in the list are mutually exclusive. If we wish to provide the user with a choice of multiple selections, we must provide a group of check boxes instead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.134.151