【发布时间】: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();
}
}
}
【问题讨论】:
-
您遇到了什么问题?
-
请看我的最新进展。并希望您能提供一些建议。谢谢!