ASP表单中的多个复选框

时间:2014-09-15 15:27:21

标签: asp.net .net forms checkbox

首先,请记住,我是一名专业设计师和自学成才的开发人员。因此,缺乏对发展中部分的了解。

我使用.ascx和.ascx.cs背后的代码创建了一个表单。我已成功使用此表单一段时间,使用基本文本字段,验证并在提交后收到包含选择的电子邮件。我最近不得不在表单中添加多个复选框。我做了一些研究,并使用下面的代码。它在网站的前端工作得很好,但是当提交表单时,我只能在我的电子邮件中找到其中一个选项。如何在后面的代码中设置代码,以便在电子邮件中收到多个选项?

这是.ascx代码:

<asp:CheckBoxList ID="CheckBoxWebsolutions" runat="server" AutoPostBack="false" TextAlign="Right" >
<asp:ListItem Value="Website Design">Website Design</asp:ListItem>
<asp:ListItem Value="Content Management System">Content Management System</asp:ListItem>
<asp:ListItem Value="Web App Development">Web App Development</asp:ListItem>
<asp:ListItem Value="Web Hosting">Web Hosting</asp:ListItem>
</asp:CheckBoxList>

这是.ascx.cs文件:

sbFormResult.Append(this.FormatAsTableRow("Send me more info on your Web Solutions:", CheckBoxWebsolutions.Text.ToString()));
sbFormResult.Append(this.FormatAsTableRow("Send me more info on your Email & Communications 1:", CheckBoxCommunications1.Text.ToString()));
sbFormResult.Append(this.FormatAsTableRow("Send me more info on your Email & Communications 2:", CheckBoxCommunications2.Text.ToString()));
sbFormResult.Append(this.FormatAsTableRow("Send me more info on your SharePoint Services:", CheckBoxCollaboration.Text.ToString()));

以下是电子邮件回复:

Test
Full Name:  John Doe
Company Name:   ABC Company
Email:  jdoe@gmail.com
Phone Number:   1234567890
Comments:   Testing
Send me more info on your Web Solutions:    Website Design
Send me more info on your Email & Communications 1: Exchange
Send me more info on your Email & Communications 2: Hosted Email Encryption
Send me more info on your SharePoint Services:  SharePoint Assessment & Planning

2 个答案:

答案 0 :(得分:1)

如果您需要显示多个回复,可以使用此伪代码(抱歉,未经测试):

Dim strSelectedWebSolutions as String = ""

For Each item as ListItem in CheckBoxWebsolutions.Items
    If item.Selected Then strSelectedWebSolutions = strSelectedWebSolutions & " " & item.Text
Next item

sbFormResult.Append(this.FormatAsTableRow("Send me more info on your Web Solutions:", strSelectedWebSolutions));

答案 1 :(得分:0)

根据您的问题,您希望收集所选CheckBox的文本。

var sbFormResult = new StringBuilder();

// Join by comma
string webSolutions = string.Join(",",
    CheckBoxWebsolutions.Items.Cast<ListItem>()
        .Where(x => x.Selected)
        .Select(x => x.Text));

sbFormResult.Append(this.FormatAsTableRow(
    "Send me more info on your Web Solutions:", webSolutions));