timnancyk
timnancyk

Reputation: 1

Multiple Checkboxes in ASP Form

First, lets keep in mind that I am a professional designer and self-taught developer. So excuse the lack of knowledge on the developing part.

I have created a form using .ascx and the code behind .ascx.cs. I have been using this form successfully for awhile with basic text fields, validations and receive an email with selections once submitted. I recently had to add multiple checkboxes to the form. I did a little research and used the code below. It works great on the front-end of the site but when the form is submitted I only get one of the options in my email. How do I set the code up in the code behind so that I receive multiple selections in my email?

Here is the .ascx code:

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

Here is the .ascx.cs file:

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()));

And here is the email response:

Test
Full Name:  John Doe
Company Name:   ABC Company
Email:  [email protected]
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

Upvotes: 0

Views: 249

Answers (2)

Win
Win

Reputation: 62260

Based on your question, you want to collect text of selected CheckBoxes.

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

Upvotes: 0

tezzo
tezzo

Reputation: 11105

If you need to display multiple responses you can use this pseudo-code (sorry, not tested):

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

Upvotes: 1

Related Questions