I have a dropdown list which contains values from 1 to 10. By default it's value is 5. So I have to create 5 dynamic radiobuttonlist on page load. And then after selecting some values on this radiobuttonlist, I have to change the value on dropdwonlist to any other number (say 8). Then the system should save the values of previously selected radiobuttonlists (5nos) to a database and then have to create new set of radiobuttonlist (8nos). But when I'm trying like below, on the Ispostback, the value of old radiobuttonlist is getting as null. Please give a solution.
public static RadioButtonList rb_list ;
public static int number_of_rb_list ;
if (!IsPostBack)
{
number_of_rb_list= Convert.ToInt32(ddl_list.SelectedItem.Value.ToString());
CreateRadioButtonList(); //have to create 6 radiobuttonlist
}
if (IsPostBack)//when changing the value of dropdownlist,
{
SaveCurrentValues();//have to save the current radiobuttonlist selected value to database
number_of_rb_list= Convert.ToInt32(ddl_list.SelectedItem.Value.ToString());//new set of radiobuttonlist
CreateRadioButtonList(number_of_rb_list= ); //have to create new set of radiobuttonlist with empty selected value
}
SaveCurrentValues()
{
for(int i=1;i<=number_of_rb_list;i++)
{
rb_list = (RadioButtonList)Page.FindControl("RadioButtonList" + i); //it is getting as null here
string ai= rb_list.SelectedValue;
//insert query
}
}
CreateRadioButtonList()
{
for(int i=1;i<=number_of_rb_list;i++)
{
rb_list = new RadioButtonList();
rb_list.ID = "RadioButtonList" +number_of_rb_list;
rb_list.RepeatColumns = 5;
rb_list.RepeatDirection = RepeatDirection.Horizontal;
rb_list.RepeatLayout = RepeatLayout.Table;
rb_list.Items.Add(new ListItem("", "0"));
rb_list.Items.Add(new ListItem("", "1"));
rb_list.Items.Add(new ListItem("", "2"));
rb_list.Items.Add(new ListItem("", "3"));
rb_list.Items.Add(new ListItem("", "4"));
rb_list.CssClass = "style_radio";
rb_list.Width = 500;
}
}
I believe the issue is that you're using a for loop on your radio buttons list. so it will return all radio buttons and not just the selected one. Try this: