如何通过代码在表格中动态添加单选按钮、文本框和按钮?
我正在尝试动态创建一个表,每行上都有单选按钮、文本框和按钮,具体取决于带有两个 TableCell 的 TableRow 左侧的问题。
到目前为止,我已经能够将问题添加到 TableRow 的左侧。现在,我很难填写右侧。
有人可以帮助我吗?
我有以下代码:
private void DesignQuestionnaire(string[] questionList, Label question, RadioButtonList answerChoices, RadioButton choices, TextBox textAnswer, Button save, Button cancel)
{
Table formTable = new Table();
TableRow formRow;
TableCell formCell;
for (int row = 0; row < questionList.Length; row++ )
{
formRow = new TableRow();
formTable.Rows.Add(formRow);
for (int col = 0; col < 2; col++ )
{
formCell = new TableCell();
//formCell.Attributes.CssStyle.Add("border", "solid");
if (col == 1)
{
formCell.ID = "A" + row.ToString();
formCell.Controls.Add(choices);
}
else
{
formCell.ID = "Q" + row.ToString();
formCell.Text = questionList.GetValue(row).ToString();
}
formRow.Cells.Add(formCell);
}
}
Controls.Add(formTable);
}
I am trying to dynamically create a table with radiobuttons, textboxes and buttons on each rows uniquely depending on the question to the left of the TableRow with two TableCells.
So far, I was able to add the questions to the left of the TableRow. Now, I am having a hard time filling out the right side of it.
Can someone help me?
I have the following code below:
private void DesignQuestionnaire(string[] questionList, Label question, RadioButtonList answerChoices, RadioButton choices, TextBox textAnswer, Button save, Button cancel)
{
Table formTable = new Table();
TableRow formRow;
TableCell formCell;
for (int row = 0; row < questionList.Length; row++ )
{
formRow = new TableRow();
formTable.Rows.Add(formRow);
for (int col = 0; col < 2; col++ )
{
formCell = new TableCell();
//formCell.Attributes.CssStyle.Add("border", "solid");
if (col == 1)
{
formCell.ID = "A" + row.ToString();
formCell.Controls.Add(choices);
}
else
{
formCell.ID = "Q" + row.ToString();
formCell.Text = questionList.GetValue(row).ToString();
}
formRow.Cells.Add(formCell);
}
}
Controls.Add(formTable);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常使用
Repeater
控件来处理这种情况。在 aspx 中,您将拥有类似的内容:
在隐藏代码中,您将拥有:
在页面加载中,仅当它不是 PostBack 时
稍后
注意 aspx 中的
OnItemDataBound
:这意味着将为问题列表中的每个项目调用R1_ItemDataBound
。I usually handle this kind of situation using a
Repeater
Control.In the aspx, you would have something like that :
In the code-behind, you would have :
In the page load, only if it is not a PostBack
And later on
Note the
OnItemDataBound
in the aspx : this means thatR1_ItemDataBound
will be called for each item in your list of questions.