我正在动态地创建多组单选按钮。每个组都有唯一的组名。当用户单击提交按钮时,我希望获得在每个组中选择的单选按钮的值。我使用以下代码来生成:
RadioButton button = new RadioButton();
button.Text = btn.getName();
button.Checked = false;
button.GroupName = btn.getBtnGroupID();
Panel1.Controls.Add(button);如何使用FindControl或alternatives在每个按钮组中获取选中/选中的按钮?
发布于 2019-01-23 13:48:49
public static string GetRadioButtonValue(ControlCollection ctrlColl, string groupName)
{
var selectedRbtn= controls.OfType<RadioButton>().FirstOrDefault(rb => rb.GroupName == groupName && rb.Checked);
return selectedRbtn== null ? string.Empty :selectedRbtn.Attributes["Value"];}
发布于 2019-01-23 13:52:03
您可以简单地执行以下操作:
RadioButton btnTest = (RadioButton)Panel1.FindControl("radioButtonId");
if (btnTest.Checked == false) {
// do something
}发布于 2019-01-23 14:12:11
尝尝这个
private List<RadioButton> rblist = new List<RadioButton>();
private void GetCheckedRB(Panel pnl, string groupName)
{
foreach (Control ctrl in pnl.Controls)
{
if (ctrl is RadioButton)
{
RadioButton rb = (RadioButton)ctrl;
if (rb.GroupName == groupName && rb.Checked)
rblist.Add(rb);
}
}
//Put action here
//MessageBox.Show(String.Join(", ", rblist.Select(x => x.Name).ToArray().ToString()));
}https://stackoverflow.com/questions/54320548
复制相似问题