Search code examples
c#asp.netradiobuttonlist

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


Solution

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