比如有10个radiobutton,他们的groupname相同,我想得到选中的radiobutton的值,请问用数组的方式怎么遍历这10个radiobutton,并得到选中的radiobutton的值。当然用radiobuttonlist可以实现,我想微软也是用的数组的方式把这中间的过程给封装了,所以就有了radiobuttonlist。但我想用数组实现下,请问这个该怎么实现,是用 list 还是用其他的数组?
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的。