ASP.net创建动态单选按钮列表

时间:2012-02-09 08:08:19

标签: c# asp.net radiobuttonlist

我需要在我的表中创建动态单选按钮。我在default.aspx(id = table1)中有一个表但在.cs中我不能访问table1这是第一个问题。如果我能达到它,我想创建动态单选按钮列表。例如,我想创建8个单选按钮列表,其中包含5个成员。我想我是用foreach块做的。我找到了这个代码示例:

foreach (?)
{
    RadioButton radioButton = new RadioButton();
    radioButton.Text = answer.Text;
    radioButton.GroupName = question.Id.ToString();
    radioButton.ID = question.Id + "_" + answer.Id;

    TableRow answerRow = new TableRow();
    TableCell answerCell = new TableCell();
    TableCell emptyCell = new TableCell();

    emptyCell.ColumnSpan = 2;

    answerCell.Controls.Add(radioButton);
    answerRow.Cells.Add(emptyCell);
    answerRow.Cells.Add(answerCell);

    table.Rows.Add(answerRow);
}

但我不知道reallu.thanks回答......

1 个答案:

答案 0 :(得分:2)

  

我需要在表格中创建动态单选按钮。我有一张桌子   default.aspx(id = table1)但在.cs中我不能访问table1   问题。

对表使用runat="server"属性:

<table id="table1" runat="server"">
</table>

从代码中,您可以动态添加行和单元格。例如:

for (int j = 0; j < 5; j++)
{
    HtmlTableRow row = new HtmlTableRow();
    for (int i = 0; i < 3; i++)
    {
        HtmlTableCell cell = new HtmlTableCell();
        RadioButton radioButton = new RadioButton();
        radioButton.Text = "Text " + i.ToString();
        cell.Controls.Add(radioButton);
        row.Cells.Add(cell);
    }
    table1.Rows.Add(row);
}
相关问题