Different Ways to Bind or Populate RadioButtonList Control In Asp.Net

How to Bind or Populate Asp.Net RadioButtonList Control

November 21, 2020

In this article we will cover different ways to Bind or Populate RadioButtonList Control in Asp.Net

<asp:RadioButtonList ID="ddlGender" runat="server"></asp:RadioButtonList>

Example 1

List<Gendergenders = new List<Gender>()
{
    new Gender(){ Name = "Male",Value = "M"},
    new Gender(){ Name = "Female",Value = "F"}
};
 
ddlGender.DataSource = genders;
ddlGender.DataTextField = "Name";
ddlGender.DataValueField = "Value";
ddlGender.DataBind();

Example 2

ddlGender.Items.Add(new ListItem() { Text = "Male", Value = "M" });
ddlGender.Items.Add(new ListItem() { Text = "Female", Value = "F" });

Example 3

// (Use this method when items needs to be added on perticular index)
ddlGender.Items.Insert(1, new ListItem() { Text = "Male", Value = "M" });
ddlGender.Items.Insert(2, new ListItem() { Text = "Female", Value = "F" });

Post Comments(0)

Leave a reply

Will not be displayed in comment box .

Loading...