Hi,
I've got a page where a list of radiobuttons are added during run time, I need to take some action on a button click(also added on runtime) depending upon the selection made...
How do I achieve this???
Radiobuttons added on pageload....
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Lalit MRank #1,186 · 1.5K reputationPosted Jan 18, 2010, 2:10 AM EST
public void AddRadioButtons() { RadioButton rBut1 = new RadioButton(); rBut1.Location = new Point(50, 50); rBut1.AutoCheck = true; rBut1.Text = "Checked"; rBut1.Checked = true; rBut1.CheckedChanged += new EventHandler(rBut_CheckedChanged); Controls.Add(rBut1); RadioButton rBut2 = new RadioButton(); rBut2.Location = new Point(50, 80); rBut2.AutoCheck = true; rBut2.Text = "UnChecked"; rBut2.Checked = false; rBut2.CheckedChanged += new EventHandler(rBut_CheckedChanged); Controls.Add(rBut2); } private void rBut_CheckedChanged(object sender, System.EventArgs e) { if (sender is RadioButton) { RadioButton radiobutton = sender as RadioButton; if (radiobutton.Checked) { radiobutton.Text = "Checked"; } else { radiobutton.Text = "UnChecked"; } } }paul daniel189 reputationPosted Jan 18, 2010, 3:13 AM EST
Try this code
if (TextBox2.Text != string.Empty)
{
int rbCount = Convert.ToInt32(TextBox2.Text);
RadioButton[] radioButtons = new RadioButton[rbCount];
for (int i = 0; i < rbCount; ++i)
{
radioButtons[i] = new RadioButton();
radioButtons[i].GroupName = "rb";
radioButtons[i].Text = Convert.ToString(i);
PlaceHolder1.Controls.Add(radioButtons[i]);
}
}
Radio Button List:
if (TextBox2.Text != string.Empty)
{
int rbCount = Convert.ToInt32(TextBox2.Text);
RadioButtonList rbList = new RadioButtonList();
for (int i = 1; i <= rbCount; ++i)
{
rbList.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
}
PlaceHolder1.Controls.Add(rbList);
}