如何通过代码在表格中动态添加单选按钮、文本框和按钮?

发布于 2024-09-28 16:26:45 字数 1260 浏览 8 评论 0原文

我正在尝试动态创建一个表,每行上都有单选按钮、文本框和按钮,具体取决于带有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘紸啶 2024-10-05 16:26:46

我通常使用 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>";
             }
          }
       }   

注意 aspx 中的 OnItemDataBound :这意味着将为问题列表中的每个项目调用 R1_ItemDataBound

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文