views:

39

answers:

1

Hi Everyone,

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

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);
    }
A: 

I usually handle this kind of situation using a Repeater Control.

In the aspx, you would have something like that :

<asp:Repeater ID="myRepeater" runat="server" OnItemDataBound="R1_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>

<ItemTemplate>
<tr>
    <td>
        <asp:Literal id="litQuestion" runat="server">
    </td>
    <td>
        <asp:PlaceHolder id="phRow" runat=server"/>
    </td>
<td>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

In the code-behind, you would have :

In the page load, only if it is not a PostBack

myRepeater.DataSource = myQuestions; // myQuestions would be a list of questions, for instance
myRepeater.DataBind();

And later on

void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

          // This event is raised for the header, the footer, separators, and items.

          // Execute the following logic for Items and Alternating Items.
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
             string question = (string)e.Item.DataItem;
             Literal litQuestion = (Literal) e.Item.FindControl("litQuestion");
             litQuestion.Text = question;

             PlaceHolder phRow = (PlaceHolder) e.Item.FindControl("phRow");

             if (question.StartsWith("something")){
                 phRow.Controls.Add(new RadioButton("blabla"));
             }

             if (((Evaluation)e.Item.DataItem).Rating == "Good") {
                ((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
             }
          }
       }   

Note the OnItemDataBound in the aspx : this means that R1_ItemDataBound will be called for each item in your list of questions.

tsimbalar
Hi tsimbalar, I just read your response. I don't seem to understand how I could create Repeater in code. Thanks for the reply though.
janejanejane
my mistake, I should have typed `asp:Repeater` instead of just `Repeater` ...
tsimbalar