ASP.Net单选按钮列表

ASP.Net单选按钮列表,asp.net,Asp.net,我正在尝试创建一个反馈表单,我有一个类似于此的模拟代码 for (int i = 1; i < 3; i++) { TableRow tr = new TableRow(); Table1.Rows.Add(tr); TableCell QuestionCell = new TableCell(); // get the text for the question and stick it in the ce

我正在尝试创建一个反馈表单,我有一个类似于此的模拟代码

    for (int i = 1; i < 3; i++)
    {
        TableRow tr = new TableRow();
        Table1.Rows.Add(tr);
        TableCell QuestionCell = new TableCell();

        // get the text for the question and stick it in the cell
        QuestionCell.Text = "<b>"+i+".</b> Question " + i;
        tr.Cells.Add(QuestionCell);

        TableRow tr2 = new TableRow();
        Table1.Rows.Add(tr2);

        // create a cell for the choice

        TableCell ChoicesCell = new TableCell();
        ChoicesCell.Width = 1000;

        // align the choices on the left
        ChoicesCell.HorizontalAlign = HorizontalAlign.Left;
        tr2.Cells.Add(ChoicesCell);

        RadioButtonList rbl = new RadioButtonList();
        rbl.ID = "Radio1_" + i;
        ChoicesCell.Controls.Add(rbl);

        rbl.Items.Add("1");
        rbl.Items.Add("2");
        rbl.Items.Add("3");
        rbl.Items.Add("4");
    }

这似乎不起作用。。。我不知道我哪里出错了

这些控件是否有EnableViewState=false。如果控件的ViewState设置为false,则服务器在回发后无法看到用户更改的控件状态。您是否为这些控件启用了EnableViewState=false。如果控件的ViewState设置为false,服务器在回发后无法看到用户更改的控件状态,因为您正在以自己的方式创建控件(几乎是动态的),您需要在page_Init中的每个页面请求中重新创建这些控件,并自己维护状态。

因为您正在以自己的方式创建控件(几乎是动态的)您需要在page_Init中重新创建这些页面请求,并自己维护状态。

我认为您必须将
控件
投射到循环中才能成为
RadioButtonList

另外,尝试使用
SelectedValue
属性,而不是
selected

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (TableCell cell in Table1.Rows)
    {
        foreach (Control ctrl in cell.Controls)
        {
            if (ctrl is RadioButtonList)
            {
                string selected = ((RadioButtonList)ctrl).SelectedValue;
            }
        }
    }
}

我认为您必须将
控件
投射到循环中,才能成为
RadioButtonList

另外,尝试使用
SelectedValue
属性,而不是
selected

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (TableCell cell in Table1.Rows)
    {
        foreach (Control ctrl in cell.Controls)
        {
            if (ctrl is RadioButtonList)
            {
                string selected = ((RadioButtonList)ctrl).SelectedValue;
            }
        }
    }
}

当你说“这不起作用”时,你是什么意思?你得到了一个具体的错误吗?当你说“这不起作用”时,你是什么意思?你得到了一个具体的错误吗?+1这对海报来说肯定是有效的。重要的是要提到.NET语言区分大小写,并且控件的属性(例如RadioButtonList或CheckBox)无法通过基类型访问。因此,没有
ctrl.selected
ctrl.selected
,而是
((复选框)ctrl).Selected
,或作为您发布的RadioButtonList。+1这肯定适用于海报。必须指出的是,.NET语言区分大小写,并且控件的属性(例如RadioButtonList或CheckBox)无法通过基类型访问。因此,没有
ctrl.selected
ctrl.selected
,而是作为
((复选框)ctrl).selected
,或作为您发布的RadioButton列表。