ASP.net creating dynamic Radio Button List

六眼飞鱼酱① 提交于 2020-01-17 07:07:07

问题


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. 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. I think I do this with foreach block. 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...


回答1:


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.

use runat="server" attribute to table:

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


来源:https://stackoverflow.com/questions/9207315/asp-net-creating-dynamic-radio-button-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!