0

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 wonder if you could show us the relevant aspx markup? Commented Feb 9, 2012 at 8:16

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

And I need to 3 radio button list which have 5 members.
if you set AutoPostback to true, it will adds javascript code to html, which automatically rises post back. So it depends on your business logic.
for (int j = 0; j <3; j++) { HtmlTableRow row = new HtmlTableRow(); HtmlTableCell cell = new HtmlTableCell(); RadioButtonList radioButtonList = new RadioButtonList(); radioButtonList.RepeatDirection = RepeatDirection.Horizontal; for (int i = 0; i < 5; i++) { radioButtonList.Items.Add(i.ToString()); cell.Controls.Add(radioButtonList); row.Cells.Add(cell); } myplace.Rows.Add(row); } this is work thanks for answering

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.