Posted by: Rhonda Tipton | November 27, 2006

RadioButtonList Control in ASP.NET

There are several list controls available to ASP.NET. They are the DropDownList, ListBox, CheckBoxList, RadioButtonList and BulletedList controls. The one I will go over in this post is the RadioButtonList control.

The ASP.NET RadioButtonList control is used to create a defined list of options in which a user can choose from. Like the DropDownList control, only one item can be selected at a time. Each selectable item in a RadioButtonList control is considered a ListItem element. This control can be loaded manually through ASP.NET code (like my example) as well as from a data source.

The RadioButtonList control is part of the System.Web.UI.WebControls namespace.

Most common properties

AutoPostBack
A Boolean value that specifies whether the form should be posted immediately after one of the items changes select status or not.

CellPadding
The space, in pixels, between the cell walls and the radio button group

DataSource
The data source to use

DataTextField
A field in the data source to be displayed in the radio button group

DataValueField
A field in the data source that specifies the value of each selectable item in the radio button group

id — A unique id for the control

OnSelectedIndexChanged
The name of the function to be executed when one of the items changes select status

RepeatColumns
The number of columns to use when displaying the radio button group.

RepeatDirection
Specifies whether the radio button group should be repeated horizontally or vertically. Legal values are “Horizontal” and “Vertical”.

RepeatLayout
The layout of the radio button group. Can be “Table” or “Flow”.

runat
Specifies that the control is a server control. Must be set to “server”

TextAlign
On which side of the radio button the text should appear (right or left)

-

Simple Example of the CheckBoxList Control

This example fills the control and displays the Selected Item in a label control when the button is accessed.

ASP.NET SOURCE

<asp:RadioButtonList ID="rblPlanets" runat="server" Width="109px">
    <asp:ListItem Value="1">Mercury</asp:ListItem>
    <asp:ListItem Value="2">Venus</asp:ListItem>
    <asp:ListItem Value="3">Earth</asp:ListItem>
    <asp:ListItem Value="4">Mars</asp:ListItem>
    <asp:ListItem Value="5">Jupiter</asp:ListItem>
    <asp:ListItem Value="6">Saturn</asp:ListItem>
    <asp:ListItem Value="7">Uranus</asp:ListItem>
    <asp:ListItem Value="8">Neptune</asp:ListItem>
</asp:RadioButtonList>

C# SOURCE

protected void btnSubmit_Click(object sender, EventArgs e)
{
  lblText.Text = "You selected: " + rblPlanets.SelectedItem.Text;
}

You can also fill the RadioButtonList control dynamically using the Page load event instead of ASP.NET code.

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    rblPlanets.Items.Add("Mercury");
    rblPlanets.Items.Add("Venus");
    rblPlanets.Items.Add("Earth");
    rblPlanets.Items.Add("Mars");
    rblPlanets.Items.Add("Jupiter");
    rblPlanets.Items.Add("Saturn");
    rblPlanets.Items.Add("Uranus");
    rblPlanets.Items.Add("Neptune");
  }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
  lblText.Text = "You selected: " + rblPlanets.SelectedItem.Text;
}

RESULT

As I mentioned at the beginning of this post, there are several list controls in ASP.NET. I plan on touching on the other list controls in future posts.

Related Content


Leave a response

You must be logged in to post a comment.

Categories