Adding an "How did you hear about us?" input field

Hi I want to put in a “How did you hear about us?” input field to my contact form and add categories to it. I attached my vb code behind and HTML file and a sample image of what I am talking about.FORMA 2.html (7.4 KB)

This is a sample of my contact form in my HTML how I can format it to make it into an input field? and have categories.

<tr>
                        <td>
                            <asp:TextBox  placeholder="Your name"  ID="txtname" runat="server"></asp:TextBox>       
                        </td>
                        <td class="auto-style2">
                             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                                 runat="server"
                                 ErrorMessage="Name is required"
                                 ControlToValidate="txtname"
                                 Text="*"
                                 ForeColor="Red"
                                 ></asp:RequiredFieldValidator>
                        </td>
                    </tr>

This is my code behind:

Try
            Dim mailMessage As New MailMessage()
            mailMessage.From = New MailAddress(txtEmail.Text)
            mailMessage.[To].Add("arobb@iogproducts.com")
            mailMessage.Subject = txtSubject.Text

            mailMessage.Body = "<b>Sender Name : </b>" + txtname.Text + "<br/>" + "<b>Sender Email : </b>" + txtEmail.Text + "<br/>" + "<b>Phone : </b>" + txtPhone.Text + "<br/>" + "<b>Subject : </b>" + txtSubject.Text + "<br/>" + "<b>Company : </b>" + txtCompany.Text + "<br/>" 
            mailMessage.IsBodyHtml = True

            Dim smtpClient As New SmtpClient("relay-hosting.secureserver.net", 25)
            smtpClient.Send(mailMessage)

            Label1.Text = "Thanks for contacting us"
            Label1.ForeColor = System.Drawing.Color.Blue

            txtname.Enabled = False
            txtEmail.Enabled = False
            txtSubject.Enabled = False
            Button1.Enabled = False
        Catch generatedExceptionName As Exception
            ' Log - Event Viewer or table 
            Label1.ForeColor = System.Drawing.Color.Blue
            Label1.ForeColor = System.Drawing.Color.Red
            Label1.Text = "There is an unknown problem. Please try later"
        End Try
    End Sub

And what exactly are you stuck on? Can you give us more details, as the markup should be simply enough, I believe it is <asp:DropDown runat=“server” id=“ddlCategory”> with a set of ListItems

Your code behind would reference that element and put it in the Email Message via concatenation.

ok should it be like this?

 <tr>
                     <td>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                         <asp:listItem Value ="1">MALE</asp:listitem>
                         <asp:listItem Value ="1">GALE</asp:listitem>
                         </asp:DropDownList>       
                    </td>
                </tr>

CODE BEHIND:

  • Country : ” + ddlCountries.SelectedValue

Yes, very similar, just make sure the ID you use in the markup is the ID you use in the CodeBehind.

1 Like

I have two issues now: first I am trying to add “Address” input but it does not read it on my codebehind, I get this error:

Compiler Error Message: BC30456: 'Address' is not a member of 'System.Net.Mail.MailMessage'.

This is how I have the codebehind:

 Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Try
            Dim mailMessage As New MailMessage()
            mailMessage.From = New MailAddress(txtEmail.Text)
            mailMessage.[To].Add("arobb@iogproducts.com")
            mailMessage.Address = txtAddress.Text

            mailMessage.Body = "<b>Sender Name : </b>" + txtname.Text + "<br/>" + "<b>Sender Email : </b>" + txtEmail.Text + "<br/>" + "<b>Phone : </b>" + txtPhone.Text + "<br/>" + "<b>Address : </b>" + txtAddress.Text + "<br/>" + "<b>Company : </b>" + txtCompany.Text + "<br/>"  + "<b>Country : </b>" + DropDownList1.SelectedValue
            mailMessage.IsBodyHtml = True

            Dim smtpClient As New SmtpClient("relay-hosting.secureserver.net", 25)
            smtpClient.Send(mailMessage)

            Label1.Text = "Thanks for contacting us"
            Label1.ForeColor = System.Drawing.Color.Blue

            txtname.Enabled = False
            txtEmail.Enabled = False
            txtSubject.Enabled = False
            Button1.Enabled = False
        Catch generatedExceptionName As Exception
            ' Log - Event Viewer or table 
            Label1.ForeColor = System.Drawing.Color.Blue
            Label1.ForeColor = System.Drawing.Color.Red
            Label1.Text = "There is an unknown problem. Please try later"
        End Try
    End Sub

Second issue is I dont get the How Did You Hear About Us? in the email. This is how I have it:

                        <asp:DropDownList placeholder="test"  ID="DropDownList1" runat="server">
                         <asp:listItem value="" disabled selected>How Did You Hear About Us?</asp:listitem>    
                         <asp:listItem Value ="1">Search Engine</asp:listitem>
                         <asp:listItem Value ="1">Email</asp:listitem>
                         <asp:listItem Value ="1">Amazon</asp:listitem>
                         <asp:listItem Value ="1">Previous User</asp:listitem>
                         <asp:listItem Value ="1">Referral</asp:listitem>
                         </asp:DropDownList>       
                    </td>
                </tr>

What are you trying to achieve with this line? As this is why you are getting the error:

And I see you have your How did you hear about us done properly.

"<b>Country : </b>" + DropDownList1.SelectedValue

But you are labeling it as “Country”, so you may want to alter that wording.

I have an address input in my form thats why. It sends it as “Subject” instead of address…

Your subject line used to be

mailMessage.Subject = txtSubject.Text

which for some odd reason you changed to

mailMessage.Address = txtAddress.Text

Oh so there is no such thing? because I want it to be Address and not Subject. I am not using a Subject input…I figured I would change it to Address.

So you want the value of txtAddress.Text to be in the Subject of your email?

Well I figured that since I was not going to use “Subject” anymore that I was suppose to change everything from Subject to Address?

Okay, in that case, you would simply change the txtSubject to txtAddress, leave it as mailMessage.Subject and leave the .Text, so you end up with
mailMessage.Subject = txtAddress.Text

1 Like

Oh I see thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.