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.

Search Flipkart Products:
Flipkart.com

No comments: