user249375
user249375

Reputation:

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. so, there's 3 rows of questions. 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?.

SCENARIO: In the same table i have the column QUESTIONTEXT i also have a TYPEID column with numeric data. Either a 1 or a 2

the idea is that i will know what control to generate for that specific question by the TYPEID number

1 for text

2 for checkbox

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

Upvotes: 5

Views: 8893

Answers (1)

Özgür Kara
Özgür Kara

Reputation: 1333

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

            }
        }

Upvotes: 6

Related Questions