protected void Button1_Click(object sender, EventArgs e)
{
RadioButton selectedRadioButton = new RadioButton();
GetSelectedRadioButton(Page.Controls,ref selectedRadioButton);
if (selectedRadioButton.ID != null)
{
Response.Write("selected radiobutton ID=" + selectedRadioButton.ID + "; state:" + selectedRadioButton.Checked.ToString());
}
}
private void GetSelectedRadioButton(ControlCollection cc,ref RadioButton rbb)
{
foreach (Control ctrl in cc)
{
if (ctrl is RadioButton)
{
RadioButton rb = (RadioButton)ctrl;
if (rb.GroupName == "rbgp")
{
if (rb.Checked)
{
rbb=rb;
}
}
}
else if (ctrl.HasControls())
{
GetSelectedRadioButton(ctrl.Controls,ref rbb);
}
}
}
像这样做就可以了,不需要用到数组或者是list的。
遍历所有radiobutton就可以实现呀
当然数组或者是lis一样可以实现
你可以把radiobutton的ID写到数组里,然后检查它们的selected属性,试试吧