[英]Add controls to a html table in code behind
I have a table column name called QUESTIONTEXT where there are 3 questions in that column. 我有一个名为QUESTIONTEXT的表列名称,该列中有3个问题。 so, there's 3 rows of questions.
所以,有3行问题。 I am adding those questions to the first column which is working like a charm.
我将这些问题添加到第一列,它就像一个魅力。 Now, i want to add a text box or a checkbox or radio button either next to it in the same cell or to the next column in the same row.
现在,我想在同一单元格中的旁边添加一个文本框或复选框或单选按钮,或者添加到同一行中的下一列。
I have 1 question. 我有一个问题。
1: How to add a control to either in the same row as the text i am currently adding or to the next column in the same row in the table?. 1:如何将控件添加到与我当前添加的文本相同的行中,或者添加到表中同一行的下一列?
SCENARIO: In the same table i have the column QUESTIONTEXT i also have a TYPEID column with numeric data. 场景:在同一个表格中,我有QUESTIONTEXT列,我还有一个带有数字数据的TYPEID列。 Either a 1 or a 2
1或2
the idea is that i will know what control to generate for that specific question by the TYPEID number 我的想法是,我将知道TYPEID号为该特定问题生成什么控件
1 for text 1为文本
2 for checkbox 2为复选框
I can grab the number like this: 我可以抓住这样的数字:
if (dataRow["TYPEID"].ToString() == "1")
So, for the first one its a 1 and i want to add a text box. 所以,对于第一个,它是1,我想添加一个文本框。 I just need to generate a text box or checkbox.
我只需要生成一个文本框或复选框。
Here is how i am currently adding the questions to the first column in the table. 以下是我目前如何将问题添加到表格的第一列。
TableRow tableRow;
TableCell tableCell;
foreach (DataTable dataTable in ds.Tables)
{
foreach (DataRow dataRow in dataTable.Rows)
{
tableRow = new TableRow();
tableCell = new TableCell();
TableRow tableRow2 = new TableRow();
TableCell tableCel2 = new TableCell();
tableCell.Text = dataRow["QUESTIONTEXT"].ToString();
if (dataRow["TYPEID"].ToString() == "1")
tableRow.Cells.Add(tableCell);
myTable.Rows.Add(tableRow);
}
}
I also created a enum to aid in the process but not sure if it will help! 我还创建了一个枚举来帮助这个过程,但不确定它是否会有所帮助!
public enum typeID : byte
{
text = 1,
multiple_choice = 2
};
Cant someone help? 有人帮忙吗? please!
请!
try this for adding second column in same row 试试这个在同一行添加第二列
TableRow tableRow;
TableCell tableCell;
TableCell tableCell2;
foreach (DataTable dataTable in ds.Tables)
{
foreach (DataRow dataRow in dataTable.Rows)
{
tableRow = new TableRow();
tableCell = new TableCell();
tableCell.Text = dataRow["QUESTIONTEXT"].ToString();
tableCell2 = new TableCell();
switch (dataRow["TYPEID"].ToString())
{
case "1":
Label lbl = new Label();
lbl.Text = "";
tableCell2.Controls.Add(lbl);
break;
case "2":
CheckBox chk = new CheckBox();
chk.Text = "";
tableCell2.Controls.Add(chk);
break;
}
tableRow.Cells.Add(tableCell);
tableRow.Cells.Add(tableCell2);
myTable.Rows.Add(tableRow);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.