ASP to ASP.NET V
Making a ASP.NET Contact Form
Our original Contact Form was based on the standard ASP spaghetti code posting back to itself and using the ASPMail component.
<%
if request("send") <> "" then
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = Request.Form("contactName")
Mailer.FromAddress= "email@domain.com"
Mailer.RemoteHost = "our.smtp.server"
Mailer.AddRecipient "Insert Title", "email@domain.com"
Mailer.Subject = "IT CONTACT FORM"
Mailer.BodyText = Request.Form("email")
Mailer.BodyText = Request.Form("businessName")
Mailer.BodyText = Request.Form("contactName")
Mailer.BodyText = Request.Form("PhoneNumber")
Mailer.BodyText = Request.Form("email")
Mailer.BodyText = " "
Mailer.BodyText = "This is what we need"
Mailer.BodyText = Request.Form("checkbox")
Mailer.BodyText = Request.Form("checkbox2")
Mailer.BodyText = Request.Form("checkbox3")
Mailer.BodyText = Request.Form("checkbox4")
Mailer.BodyText = Request.Form("checkbox5")
Mailer.BodyText = Request.Form("checkbox6")
Mailer.BodyText = " "
Mailer.BodyText = "This is what we have"
Mailer.BodyText = Request.Form("checkbox7")
Mailer.BodyText = Request.Form("checkbox8")
Mailer.BodyText = Request.Form("checkbox9")
Mailer.BodyText = Request.Form("checkbox10")
Mailer.BodyText = Request.Form("checkbox11")
Mailer.BodyText = " "
Mailer.BodyText = "Time Frame: " & Request.Form("timeFrame")
Mailer.BodyText = " "
Mailer.BodyText = Request.Form("messageTxt")
if Mailer.SendMail then
Response.write "thank you - your information has been sent"
else
Response.Write "Mail send failure. Error was " & Mailer.Response
end if
else
%>
<form method="post" action="contact.asp">
Your personal information will NOT be shared with any 3rd parties
<br /><br />
We'd love to hear from you:<br /><br />
Business Name:<br />
<input type="text" name="businessName" size="30" />
<br /><br />
Contact Name:<br />
<input type="text" name="contactName" size="30" />
<br /><br />
Email Address:<br />
<input type="text" name="email" size="30" />
<br /><br />
What type of time frame are you anticipating?<br>
<input type="text" name="timeFrame" size="30" />
<br /><br />
What type of project are you interested in? (check all
that apply)<br />
<input type="checkbox" name="checkbox" value="web_design" />
Website Design<br />
<input type="checkbox" name="checkbox2" value="logo_design" />
Logo Design<br />
<input type="checkbox" name="checkbox3"
value="flash_animation" />
Flash Animation<br />
<input type="checkbox" name="checkbox4" value="redesign" />
Website Redesign<br />
<input type="checkbox" name="checkbox5" value="SE_place" />
Search Engine Placement<br />
<input type="checkbox" name="checkbox6" value="ecommerce" />
Ecommerce Site<br>
<br /><br />
Do you already have any of the folloing: (check all that apply)<br />
<input type="checkbox" name="checkbox7"
value="Domain Name" />Domain Name<br />
<input type="checkbox" name="checkbox8" value="Hosting" />
Hosting<br />
<input type="checkbox" name="checkbox9" value="Printed Copy" />
Printed Copy<br />
<input type="checkbox" name="checkbox10" value="Digital Copy" />
Digital Copy<br />
<input type="checkbox" name="checkbox11"
value="Digital Images" />
Digital Images<br /><br />
Questions or comments:<br /><br />
<textarea name="messageTxt" cols="30" rows="5">
</textarea>
<br /><br />
<input type="hidden" name="send" value="notempty">
<input type="submit" value="Submit"
name="submit" />
<input type="reset" value="Clear" name="reset" />
</form>
<%end if%>
This was definitley one of those instances where I got tired of reading pretty quickly. It seems there are many, many options when creating forms, checkboxes, and emails with .NET
I read these two simple articles and sample files and went about rewriting this page pretty quickly, using some of the knowledge I had already picked up in the previous articles.
CheckBoxList Web Server Control
How to send an email in ASP.net
The second article there was simply this easy ASP.NET Email script
Dim mmMail As New System.Web.Mail.MailMessage()
Dim objSmtpServer As System.Web.Mail.SmtpMail
mmMail.From = "me@me.com"
mmMail.To = "you@you.com"
mmMail.Subject = "My subject"
mmMail.Body = "My body"
objSmtpServer.SmtpServer = "my smtp server"
objSmtpServer.Send(mmMail)
While this was useful, it didn't answer all my issues, just got me started. I also picked up that I didn't have to use the StringBuilder Class in order to build up a string as I had thought before. This time I simply used
"str +="
So in essense my previous example of how to build a string could now also be
<%@ Import Namespace="System.Net" %>
<script language="VB" runat="server">
Sub Page_Load(s as Object, e as EventArgs)
Dim str as String
str = ""
str += "abcdefgh"
str += "ijklmnopqrs"
str += "tuvwxyz"
myText.Text = str
End Sub
</script>
<asp:label id="myText" runat="server" />
This made a lot more sense to me
Not sure if i used all the proper techniques here, but I essentially put the entire form inside <asp:label ID=contactForm></asp:label>
Then a Thank You Message in another label tag.
the "If.IsPostBack" means "If the user has posted"... I think you'll get the rest
Anyways here's the code I ended up with
<Script runat="Server">
Private Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
If Page.IsPostBack Then
contactForm.Visible = False
thankYou.Visible = True
Else
contactForm.Visible = True
thankYou.Visible = False
End If
End Sub
Sub Button_Click( s As Object, e As EventArgs )
Dim weHave as String = ""
Dim weNeed as String = ""
Dim strEmail as String = ""
Dim iLoop as Integer
Dim iLoop2 as Integer
For iLoop = 0 To WeNeedList.Items.Count - 1
If WeNeedList.Items(iLoop).Selected Then
weNeed += WeNeedList.Items(iLoop).Value & ", "
End If
Next
For iLoop2 = 0 To WeHaveList.Items.Count - 1
If WeHaveList.Items(iLoop2).Selected Then
weHave += WeHaveList.Items(iLoop2).Value & ", "
End If
Next
strEmail += "Contact Name:" & contactName.Text & vbcrlf
strEMail += "Company Name: " & businessName.Text & vbcrlf
strEmail += "We need the following" & vbcrlf & weNeed
strEmail += "We have the following" & vbcrlf & weHave
strEmail += "Time Frame Expected:" & TimeFrame.Text & vbcrlf
strEmail += message.Text
Dim mmMail As New System.Web.Mail.MailMessage()
Dim objSmtpServer As System.Web.Mail.SmtpMail
mmMail.From = email.Text
mmMail.To = "someone@insert-title.com"
mmMail.Subject = "IT CONTACT FORM"
mmMail.Body = strEMail
objSmtpServer.SmtpServer = "our.smtp.server"
objSmtpServer.Send(mmMail)
End Sub
</Script>
<html>
<body bgcolor="#ffcc66">
<div align="left" id="content">
<asp:label ID="contactForm" runat="server">
<form runat="Server">
Your personal information will NOT be shared with any 3rd parties
<br /><br />
We'd love to hear from you:<br /><br />
Business Name:<br />
<asp:TextBox ID="businessName" size="30" runat="server" />
<br /><br />
Contact Name:<br />
<asp:TextBox ID="contactName" size="30" runat="server" />
<br /><br />
Email Address:<br />
<asp:TextBox ID="email" size="30" runat="server" />
<br /><br />
What type of time frame are you anticipating?<br>
<asp:TextBox ID="timeFrame" size="30" runat="server" />
<br /><br />
What type of project are you interested in? (check all
that apply)<br />
<asp:CheckBoxList ID=WeNeedList Runat=server>
<asp:ListItem Value=Web Design>Web Design</asp:ListItem>
<asp:ListItem Value=Logo Design>Logo Design</asp:ListItem>
<asp:ListItem Value=Flash Animation>
Flash Animation</asp:ListItem>
<asp:ListItem Value=Redesign>Redesign</asp:ListItem>
<asp:ListItem Value=SEO>
Search Engine Placement</asp:ListItem>
<asp:ListItem Value=Ecommerce>Ecommerce</asp:ListItem>
</asp:CheckBoxList>
<br /><br />
Do you already have any of the following:
(check all that apply)<br />
<asp:CheckBoxList ID=WeHaveList Runat=server>
<asp:ListItem Value=Domain>Domain Name</asp:ListItem>
<asp:ListItem Value=Hosting>Hosting</asp:ListItem>
<asp:ListItem Value=Printed Copy>Printed Copy</asp:ListItem>
<asp:ListItem Value=Digital Copy>
Digital Copy</asp:ListItem>
<asp:ListItem Value=Digital Images>
Digital Images</asp:ListItem>
</asp:CheckBoxList>
<br /><br />
Questions or comments:<br /><br />
<asp:textbox TextMode="Multiline" ID="message"
cols="30" rows="5" runat="server" />
<br /><br />
<asp:Button
Text="Send"
OnClick="Button_Click"
Runat="Server" />
</form>
</asp:label>
<asp:Label ID="thankYou"
runat="server">Thank You, Your Message Has Been Sent</asp:label>
</div>
</body>
</html>
And here's a cleaner copy of almost the same thing, but with less emphasis on design and fewer fields
<Script runat="Server">
'******************************************
'This Sub routine happens whent the page loads
'similar to an onLoad event you may have included
'in your body tag before
'It essentially says that if the form has NOT been submitted
'then the object called contactForm should be visible
'and the object called thankYou should not be visible
'The reverse is true if the form HAS been submitted
'******************************************
Private Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
If Page.IsPostBack Then
contactForm.Visible = False
thankYou.Visible = True
Else
contactForm.Visible = True
thankYou.Visible = False
End If
End Sub
'******************************************
'This Sub wil be called when a button clicked that references
'this Sub in an OnClick event.
'Here we create our message by building up a string for email content
'based on the fields the visitor filled out.
'
'Basically if the field had an ID=ContactName then
'to reference this we call ContactName.Text
'this is similar to how we used to use request.form("ContactName")
'
'Once all that is in place we simply use Web.Mail.MailMessage()
'and System.Web.Mail.SmtpMail to send our email
'
'Now that our form has been submitted the Page Load event is called
'again and we are only displaying the contents of the thankYou Label
'******************************************
Sub Button_Click( s As Object, e As EventArgs )
Dim strEmail as String = ""
strEmail += "Contact Name:" & contactName.Text & vbcrlf
strEMail += "Company Name: " & businessName.Text & vbcrlf
strEmail += message.Text
Dim mmMail As New System.Web.Mail.MailMessage()
Dim objSmtpServer As System.Web.Mail.SmtpMail
mmMail.From = email.Text
mmMail.To = "you@you.com"
mmMail.Subject = "CONTACT FORM"
mmMail.Body = strEMail
objSmtpServer.SmtpServer = "our.smtp.server"
objSmtpServer.Send(mmMail)
End Sub
</Script>
<html>
<body>
<asp:label ID="contactForm" runat="server">
<form runat="Server">
Business Name:<asp:TextBox ID="businessName" runat="server" />
<br />
Contact Name: <asp:TextBox ID="contactName"runat="server" />
<br />
Email Address:<asp:TextBox ID="email" runat="server" />
<br />
Questions or comments:<br />
<br />
<asp:Button Text="Send" OnClick="Button_Click" Runat="Server" />
</form>
</asp:label>
<asp:Label ID="thankYou" runat="server">
Thank You, Your Message Has Been Sent
</asp:label>
</body>
</html>