简体   繁体   English

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

[英]ASP.net creating dynamic Radio Button List

I need to create dynamic radio button in my table.I have a table in default.aspx(id =table1) but in .cs I dont access to table1 this is frist problem. 我需要在我的表中创建动态单选按钮。我在default.aspx(id = table1)中有一个表但在.cs中我不能访问table1这是第一个问题。 if I can reach it, I want to create dynamic radio button List . 如果我能达到它,我想创建动态单选按钮列表。 For example I want to create 8 radio button list which have 5 members. 例如,我想创建8个单选按钮列表,其中包含5个成员。 I think I do this with foreach block. 我想我是用foreach块做的。 I find this code samples : 我找到这个代码示例:

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);
}

but I dont know actuallu.thanks for answering... 但我不知道reallu.thanks回答...

I need to create dynamic radio button in my table.I have a table in default.aspx(id =table1) but in .cs I dont access to table1 this is frist problem. 我需要在我的表中创建动态单选按钮。我在default.aspx(id = table1)中有一个表但在.cs中我不能访问table1这是第一个问题。

use runat="server" attribute to table: 对表使用runat="server"属性:

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

From code, you can add rows and cells dynamically. 从代码中,您可以动态添加行和单元格。 For example: 例如:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM