簡體   English   中英

ASP.net創建動態單選按鈕列表

[英]ASP.net creating dynamic Radio Button List

我需要在我的表中創建動態單選按鈕。我在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回答...

我需要在我的表中創建動態單選按鈕。我在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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM