C# 中的单选按钮列表中出现一些错误

本文关键字:错误 列表 单选按钮 | 更新日期: 2023-09-27 18:36:19

for(i=1;i<=10;i++)
{
    RadioButtonList rad = new RadioButtonList();
    rad.ID = "rad" + i.ToString();
    lbl.Attributes.Add("runat", "Server");
    rad.Style.Add(HtmlTextWriterStyle.Position, "absolute");
    rad.Style[HtmlTextWriterStyle.Top] = top + 20 + "px";
    rad.Style[HtmlTextWriterStyle.Left] = "200px";
    rad.Attributes.Add("runat", "Server");
}

例如,我正在使用名为 rad1 到 rad10 的代码创建 10 个单选按钮列表我想得到 rad(1) 到 rad(10).selected来自用户的项目...

rad(i).selectItems给了我一个错误。我想获取从 rad1 到 rad10 的选定项目。例:

answer1 = rad1.selectItems.tostring();
answer2 = rad2.selectItems.tostring();
answer3 = rad3.selectItems.tostring();
answer4 = rad4.selectItems.tostring();
answer5 = rad5.selectItems.tostring();
answer6 = rad6.selectItems.tostring();
answer7 = rad7.selectItems.tostring();
answer8 = rad8.selectItems.tostring();
answer9 = rad9.selectItems.tostring();
answer10 = rad10.selectItems.tostring();

但我什至无法指定rad1.selecteditems,这给了我一个错误。 它一直说 rad(i) 在上下文中不存在

C# 中的单选按钮列表中出现一些错误

RadioButtonList rad = new RadioButtonList();

您正在用相同的名称命名所有单选按钮列表对象;

您应该创建单选按钮列表的arrayList

应使用以下表达式查找动态生成的控件:

RadioButtonList rbl = (RadioButtonList)FindControl("rad1");