1

2 つの TableCells を持つ 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);
    }
4

1 に答える 1

0

Repeater私は通常、コントロールを使用してこの種の状況を処理します。

aspx では、次のようなものがあります。

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

コード ビハインドでは、次のようになります。

ページの読み込みで、PostBack でない場合のみ

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

そして後で

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>";
             }
          }
       }   

OnItemDataBoundaspxの に注意してください。これはR1_ItemDataBound、質問リストの各項目に対して が呼び出されることを意味します。

于 2010-10-20T07:00:46.987 に答える