简体   繁体   中英

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. 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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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