【问题标题】:Please help to convert questionnaire webpage design to dynamic C# code behind请帮助将问卷网页设计转换为背后的动态C#代码
【发布时间】:2021-04-20 01:26:37
【问题描述】:

我的任务是用 C# 创建一个调查网页,我对 vb 比较熟悉。

我找到了与我需要的类似的东西。但是,我有很多调查问题,因此我想动态创建它们,但我很讨厌将 html 转换为后面的代码。

因此,我希望有人可以提供帮助。谢谢!

附加是工作网页:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm9.aspx.cs" Inherits="CSVHelperProject.WebForm9" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server">
                <HeaderTemplate>
                    <table>
                        <thead>
                            <tr>
                                <th>Questions</th>
                                <th>Strongly agree</th>
                                <th>Agree</th>
                                <th>Neutral</th>
                                <th>Disagree</th>
                                <th>Strongly disagree</th>
                            </tr>
                        </thead>
                        <tbody>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:RadioButton ID="rbnStronglyAgree" runat="server" data_value="1" GroupName='<%# Container.ItemIndex %>' />
                            <asp:HiddenField ID="HiddenField1" runat="server" Value="1" />
                        </td>
                        <td>
                            <asp:RadioButton ID="rbnAgree" runat="server" GroupName='<%# Container.ItemIndex %>' />
                            <asp:HiddenField ID="HiddenField2" runat="server" Value="2" />
                        </td>
                        <td>
                            <asp:RadioButton ID="rbnNeutral" runat="server" GroupName='<%# Container.ItemIndex %>' />
                            <asp:HiddenField ID="HiddenField3" runat="server" Value="3" />
                        </td>
                        <td>
                            <asp:RadioButton ID="rbnDisagree" runat="server" GroupName='<%# Container.ItemIndex %>' />
                            <asp:HiddenField ID="HiddenField4" runat="server" Value="4" />
                        </td>
                        <td>
                            <asp:RadioButton ID="rbnStronglyDisagree" runat="server" GroupName='<%# Container.ItemIndex %>' />
                            <asp:HiddenField ID="HiddenField5" runat="server" Value="5" />
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </tbody>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
            <asp:Button ID="btnGetResult" runat="server" Text="Get Result" OnClick="btnGetResult_Click" /><br />
            <asp:Label ID="lblMSG" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>
</html>

后面的代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CSVHelperProject
{
    public partial class WebForm9 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Bind Question
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Question"));
                dt.Rows.Add("The course description accurately described the course");
                dt.Rows.Add("The class size was appropriate.");
                dt.Rows.Add("The classes started and ended on time.");
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
            }
        }

        protected void btnGetResult_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            foreach (RepeaterItem item in Repeater1.Items)
            {
                //find question
                Label lblquestion = (Label)item.FindControl("lblQuestion");

                string answer = string.Empty;

                //Using FindControl method to find the radiobuttons
                RadioButton rbnStronglyAgree = (RadioButton)item.FindControl("rbnStronglyAgree");
                RadioButton rbnAgree = (RadioButton)item.FindControl("rbnAgree");
                RadioButton rbnNeutral = (RadioButton)item.FindControl("rbnNeutral");
                RadioButton rbnDisagree = (RadioButton)item.FindControl("rbnDisagree");
                RadioButton rbnStronglyDisagree = (RadioButton)item.FindControl("rbnStronglyDisagree");

                //Check which radiobutton is checked
                if (rbnStronglyAgree.Checked)
                {
                    HiddenField hid1 = (HiddenField)item.FindControl("HiddenField1");
                    answer = hid1.Value;
                }
                else if (rbnAgree.Checked)
                {
                    HiddenField hid2 = (HiddenField)item.FindControl("HiddenField2");
                    answer = hid2.Value;
                }
                else if (rbnNeutral.Checked)
                {
                    HiddenField hid3 = (HiddenField)item.FindControl("HiddenField3");
                    answer = hid3.Value;
                }
                else if (rbnDisagree.Checked)
                {
                    HiddenField hid4 = (HiddenField)item.FindControl("HiddenField4");
                    answer = hid4.Value;
                }
                else if (rbnStronglyDisagree.Checked)
                {
                    HiddenField hid5 = (HiddenField)item.FindControl("HiddenField5");
                    answer = hid5.Value;
                }

                sb.AppendLine(string.Format("<br />Q: {0} <br />A: {1} <br />", lblquestion.Text, answer));
            }

            lblMSG.Text = sb.ToString();
        }
    }
}

【问题讨论】:

  • 您遇到了什么问题?
  • 请看我的最新进展。并希望您能提供一些建议。谢谢!

标签: c# dynamic controls


【解决方案1】:

我找到了一个接近我需要的解决方案。 但是,原始版本需要一些调整才能使其正常工作。 Creating An HtmlTable Using An ASP.NET Repeater Dynamically Bound to an Object List 而且,下面是复制粘贴的工作副本:

网络表单

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm12.aspx.cs" Inherits="CSVHelperProject.WebForm12" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    
    </div>
    </form>
</body>
</html>

后面的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;



namespace CSVHelperProject
{
    public partial class WebForm12 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          var dogs = new List<Dog>
            {
                new Dog { Name = "Rex", Breed = "Russell Terrier" },
                new Dog { Name = "Fido", Breed = "Poodle" },
                new Dog { Name = "Fetcher", Breed = "Golden Retriever" },
            };

            Repeater repeater = new Repeater
            {
                ID = "Repeater1",
                HeaderTemplate = new CustomTemplate(ListItemType.Header),
                ItemTemplate = new CustomTemplate(ListItemType.Item),
                FooterTemplate = new CustomTemplate(ListItemType.Footer),
                DataSource = dogs
            };

            repeater.DataBind();
            PlaceHolder1.Controls.Add(repeater);
        }

        // Custom template class to add controls to the repeater's header, item and footer sections.
        private sealed class CustomTemplate : ITemplate
        {
            private ListItemType ListItemType { get; set; }

            public CustomTemplate(ListItemType type)
            {
                ListItemType = type;
            }

            public void InstantiateIn(Control container)
            {
                if (ListItemType == ListItemType.Header)
                {
                    LiteralControl table = new LiteralControl();
                    HtmlGenericControl head = new HtmlGenericControl("thead");
                    HtmlTableRow row = new HtmlTableRow();
                    row.Cells.Add(new HtmlTableCell("th") { InnerText = "Breed" });
                    row.Cells.Add(new HtmlTableCell("th") { InnerText = "Name" });
                    head.Controls.Add(row);
                    table.Text = string.Format("<table>{0}<tbody>", RenderControl(head));
                    container.Controls.Add(table);
                }
                else if (ListItemType == ListItemType.Item)
                {
                    HtmlTableRow row = new HtmlTableRow();

                    HtmlTableCell cell1 = new HtmlTableCell();
                    cell1.Controls.Add(new Literal { ID = "LiteralBreed" });
                    row.Cells.Add(cell1);

                    HtmlTableCell cell2 = new HtmlTableCell();
                    cell2.Controls.Add(new Literal { ID = "LiteralName" });
                    row.Cells.Add(cell2);

                    container.Controls.Add(row);
                    container.DataBinding += new EventHandler(Container_DataBinding);
                }
                else if (ListItemType == ListItemType.Footer)
                {
                    LiteralControl footer = new LiteralControl("</tbody></table>");
                    container.Controls.Add(footer);
                }
            }

            // Event handler to populate the dog's breed and name in the table when data-binding occurs.
            private void Container_DataBinding(object sender, EventArgs e)
            {
                RepeaterItem item = sender as RepeaterItem;
                if (item != null)
                {
                    var dog = ((Dog)item.DataItem);

                    Literal breed = (Literal)item.FindControl("LiteralBreed");
                    breed.Text = dog.Breed;

                    Literal name = (Literal)item.FindControl("LiteralName");
                    name.Text = dog.Name;
                }
            }
        }

        private sealed class Dog
        {
            public string Breed { get; set; }
            public string Name { get; set; }
        }

        public static string RenderControl(Control control)
        {
            StringBuilder stringBuilder = new StringBuilder();

            using (StringWriter stringWriter = new StringWriter(stringBuilder))
            {
                using (Html32TextWriter htmlWriter = new Html32TextWriter(stringWriter))
                {
                    control.RenderControl(htmlWriter);
                    return stringBuilder.ToString();
                }
            }
        }

    }
}

【讨论】:

    【解决方案2】:

    现在,我需要帮助将下面的 HTML 转换为后面的代码:

     <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
                            </td>
                            <td>
                                <asp:RadioButton ID="rbnStronglyAgree" runat="server" data_value="1" GroupName='<%# Container.ItemIndex %>' />
                                <asp:HiddenField ID="HiddenField1" runat="server" Value="1" />
                            </td>
                            <td>
                                <asp:RadioButton ID="rbnAgree" runat="server" GroupName='<%# Container.ItemIndex %>' />
                                <asp:HiddenField ID="HiddenField2" runat="server" Value="2" />
                            </td>
                            <td>
                                <asp:RadioButton ID="rbnNeutral" runat="server" GroupName='<%# Container.ItemIndex %>' />
                                <asp:HiddenField ID="HiddenField3" runat="server" Value="3" />
                            </td>
                            <td>
                                <asp:RadioButton ID="rbnDisagree" runat="server" GroupName='<%# Container.ItemIndex %>' />
                                <asp:HiddenField ID="HiddenField4" runat="server" Value="4" />
                            </td>
                            <td>
                                <asp:RadioButton ID="rbnStronglyDisagree" runat="server" GroupName='<%# Container.ItemIndex %>' />
                                <asp:HiddenField ID="HiddenField5" runat="server" Value="5" />
                            </td>
                        </tr>
                    </ItemTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      相关资源
      最近更新 更多