Dropdownlist Control with US States in a Contact Form

The code snippet below combines a dropdownlist 'ddlStates' which contains all the US States with a contact form. The snippet is simply intended to show you how to get the text value of the selected option and send it in an email.

If you were to use this as a real application then you would have to ensure all the fields were entered and a valid email address was provided using form validation.

Figure 1 shows how our file 'states.aspx' will display in your browser. The code for the file is shown below.

There are 3 textbox server controls called 'txtName' and 'txtEmail' and another textbox called 'txtMessage' with it's textmode property set to multiline. There is a button control 'Button1' and a label 'lblStatus' that doesn't have any initial text set when the page is first loaded and indeed it's visible property is set to 'false'. There is also a dropdownlist 'ddlStates'.

Figure 1
Dropdownlist States Contact Form

In short once the button 'Button1' is clicked the button's click event is raised and the subroutine Button1_Click executed. The dropdownlist's selection is assigned to the string variable 'State'.

An email is then sent with the contact form details, notice that the code below imports the 2 namespaces that we need in ASP.NET 2 to access and work with the SMTPClient and MailMessage classes and send a successful email.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Net.Mail.SmtpClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim State As String = ddlStates.SelectedItem.Text
If State = "" Then
State = "No State selected."
End If


Dim Msg As MailMessage = New MailMessage()
Dim MailObj As New SmtpClient
'Email address to send contact form. You can use a display name as well.
Msg.To.Add(New MailAddress("michael@codefixer.com", "Michael Wall"))
'Carbon Copy Email address to send contact form to.
Msg.CC.Add(New MailAddress("michael@codefixersoftware.com", "Michael Wall"))
'The from email address is the email entered into the txtEmail textbox.
Msg.From = New MailAddress(txtEmail.Text)
Msg.Subject = "Contact form"
Msg.Body = "Message From:" & txtName.Text & "<br />Message Text: " & txtMessage.Text & _
"<br />State selected:" & State
'Send as HTML
Msg.IsBodyHtml = "True"
MailObj.Host = "localhost" 'check with your hosting company what the mailserver name is
MailObj.Send(Msg)

lblStatus.Visible = True
lblStatus.Text = "Thank you. We will get back to you if needed."

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
lblStatus.Visible = False
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id=Head1 runat="server">
<title>Contact Form with States Dropdownlist</title>
</head>
<body>

<form id="form1" runat="server">
<div align="center">
<table style="text-align:left;">
<tr>
<td>Name</td>
<td style="width: 424px;">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Email</td>
<td style="width: 424px;">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
State</td>
<td style="width: 424px"><asp:DropDownList ID="ddlStates" runat="server" AutoPostBack="false">
<asp:ListItem Value="" Selected="True">Select a State</asp:ListItem>
<asp:ListItem value="AL">Alabama</asp:ListItem>
<asp:ListItem value="AK">Alaska</asp:ListItem>
<asp:ListItem value="AZ">Arizona</asp:ListItem>
<asp:ListItem value="AR">Arkansas</asp:ListItem>
<asp:ListItem value="CA">California</asp:ListItem>
<asp:ListItem value="CO">Colorado</asp:ListItem>
<asp:ListItem value="CT">Connecticut</asp:ListItem>
<asp:ListItem value="DC">D.C.</asp:ListItem>
<asp:ListItem value="DE">Delaware</asp:ListItem>
<asp:ListItem value="FL">Florida</asp:ListItem>
<asp:ListItem value="GA">Georgia</asp:ListItem>
<asp:ListItem value="HI">Hawaii</asp:ListItem>
<asp:ListItem value="ID">Idaho</asp:ListItem>
<asp:ListItem value="IL">Illinois</asp:ListItem>
<asp:ListItem value="IN">Indiana</asp:ListItem>
<asp:ListItem value="IA">Iowa</asp:ListItem>
<asp:ListItem value="KS">Kansas</asp:ListItem>
<asp:ListItem value="KY">Kentucky</asp:ListItem>
<asp:ListItem value="LA">Louisiana</asp:ListItem>
<asp:ListItem value="ME">Maine</asp:ListItem>
<asp:ListItem value="MD">Maryland</asp:ListItem>
<asp:ListItem value="MA">Massachusetts</asp:ListItem>
<asp:ListItem value="MI">Michigan</asp:ListItem>
<asp:ListItem value="MN">Minnesota</asp:ListItem>
<asp:ListItem value="MS">Mississippi</asp:ListItem>
<asp:ListItem value="MO">Missouri</asp:ListItem>
<asp:ListItem value="MT">Montana</asp:ListItem>
<asp:ListItem value="NE">Nebraska</asp:ListItem>
<asp:ListItem value="NV">Nevada</asp:ListItem>
<asp:ListItem value="NH">New Hampshire</asp:ListItem>
<asp:ListItem value="NJ">New Jersey</asp:ListItem>
<asp:ListItem value="NM">New Mexico</asp:ListItem>
<asp:ListItem value="NY">New York</asp:ListItem>
<asp:ListItem value="NC">North Carolina</asp:ListItem>
<asp:ListItem value="ND">North Dakota</asp:ListItem>
<asp:ListItem value="OH">Ohio</asp:ListItem>
<asp:ListItem value="OK">Oklahoma</asp:ListItem>
<asp:ListItem value="OR">Oregon</asp:ListItem>
<asp:ListItem value="PA">Pennsylvania</asp:ListItem>
<asp:ListItem value="RI">Rhode Island</asp:ListItem>
<asp:ListItem value="SC">South Carolina</asp:ListItem>
<asp:ListItem value="SD">South Dakota</asp:ListItem>
<asp:ListItem value="TN">Tennessee</asp:ListItem>
<asp:ListItem value="TX">Texas</asp:ListItem>
<asp:ListItem value="UT">Utah</asp:ListItem>
<asp:ListItem value="VT">Vermont</asp:ListItem>
<asp:ListItem value="VA">Virginia</asp:ListItem>
<asp:ListItem value="WA">Washington</asp:ListItem>
<asp:ListItem value="WV">West Virginia</asp:ListItem>
<asp:ListItem value="WI">Wisconsin</asp:ListItem>
<asp:ListItem value="WY">Wyoming</asp:ListItem>
</asp:DropDownList>

</td>
</tr>
<tr>
<td>Comments</td>
<td style="width: 424px;">
<asp:TextBox ID="txtMessage" runat="server" Columns="50" Rows="5" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="height: 26px;">
<asp:Button ID="Button1" runat="server" Text="Send Contact Form" /></td>
</tr>
</table>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</div>
</form>

</body>
</html>



The label control 'LblStatus' will display the thank you message as figure 2 below shows.

Figure 2
Dropdownlist States Contact Form

Figure 3 shows the email message that you will receive when the contact form is successfully submitted.

Figure 3
Dropdownlist Email

Just on a final note and to emphasize the fact that you should incorporate some sort of form validation.

Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.