Showing posts with label RadioButtonList. Show all posts
Showing posts with label RadioButtonList. Show all posts

Tuesday, May 22, 2012

RadioButtonList Binding options Dynamically


RadioButtonList Binding options Dynamically

In general we declare the options of a RadioButtonList control statically in the .aspx page, but what if we need to set these values at runtime based on data from a database, we shall see on how to achieve this.

The Asp.Net
RadioButtonList control allows data binding similar to a GridView or Dropdown List control, using which we can bind a data source to the RadioButtonList control.
In the .aspx page just add the RadioButtonList tag without adding any options as follows.

Select Name:
<asp:RadioButtonList
    ID="radListName"
    runat="server"
    RepeatDirection="Vertical">
</asp:RadioButtonList>

Then navigate to the code behind file and add the required options as follows.

protected void Page_Load(object sender, EventArgs e)
{
     radListName.DataSource = dtEmployee;
   radListName.DataValueField = "ID";
   radListName.DataTextField = "Name";
   radListName.DataBind();
}

That’s it we have bound the options to the RadioButtonList control at runtime.

Monday, May 21, 2012

Adding RadioButton options Dynamically


Adding RadioButton options Dynamically

In general we declare the options of a RadioButtonList control statically in the .aspx page, but what if we need to set these values at runtime, we shall see on how to achieve this.
In the .aspx page just add the RadioButtonList tag without adding any options as follows.

Select Age:
<asp:RadioButtonList
    ID="radListAge"
    runat="server"
    RepeatDirection="Vertical">
</asp:RadioButtonList>

Then navigate to the code behind file and add the required options as follows.

protected void Page_Load(object sender, EventArgs e)
{
    radListAge.Items.Add(new ListItem("10-20", "1"));
    radListAge.Items.Add(new ListItem("21-30", "2"));
    radListAge.Items.Add(new ListItem("31-40", "3"));
    radListAge.Items.Add(new ListItem("41-50", "4"));
    radListAge.Items.Add(new ListItem(">50", "5"));
}

That’s it we have added options to the RadioButtonList control at runtime.