比如有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的。