<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Contact.aspx.cs"
Inherits="Contact"
%>
DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head>
<title>Contact Ustitle>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet"
type="text/css"
href="CSS/style.css"
/>
<script language="javascript" type="text/javascript">
function
onlyNum(obj)
{
if(obj=="num"){
var reg =
/[^0-9.]/g;
document.getElementById("txtTel").value =document.getElementById("txtTel").value.replace(reg,"");
}
}
script>
head>
<body>
<form id="frmContact"
runat="server">
<asp:ScriptManager ID="scmContact"
runat="server">asp:ScriptManager>
<table cellpadding="0" cellspacing="0" style="width:100%">
<tr><td colspan="3"><div>
<table cellpadding="0" cellspacing="0" class="middleTable">
<tr>
<td valign="top" style="text-align:justify; padding-right:5px;
padding-left:5px; width: 747px;">
<table cellpadding="0" cellspacing="5" width="100%">
<tr><td><h2>Contact Ush2>td><td> td>tr>
<tr><td colspan="2" align="center"
style="
background-color:#FFFFFF; color:#112A40;" ><asp:Label ID="lblMsg" runat="server" Visible="false">asp:Label>td>tr>
<tr><td>Nametd><td><asp:TextBox ID="txtName"
CssClass="inputtxt"
runat="server"
onfocus="this.style.backgroundColor='#ff0000'"
onblur="this.style.backgroundColor=''"
>asp:TextBox><asp:RequiredFieldValidator
ID="rfvName"
runat="server"
ErrorMessage="Enter
Your Name" Display="None" ValidationGroup="vgSubmit" ControlToValidate="txtName" SetFocusOnError="True">asp:RequiredFieldValidator>td>tr>
<tr><td>Addresstd><td><asp:TextBox ID="txtAddress"
CssClass="inputtxt"
Rows="5"
onfocus="this.style.backgroundColor='#ff0000'"
onblur="this.style.backgroundColor=''"
Columns="15"
TextMode="MultiLine"
runat="server">asp:TextBox><asp:RequiredFieldValidator ID="rfvAdd" runat="server" ErrorMessage="Enter Your Address" Display="None"
ValidationGroup="vgSubmit"
ControlToValidate="txtAddress"
SetFocusOnError="True">asp:RequiredFieldValidator>td>tr>
<tr><td>Statetd><td>
<asp:UpdatePanel ID="updtpnlState"
runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlState"
CssClass="inputddl"
runat="server"
onfocus="this.style.backgroundColor='#ff0000'"
onblur="this.style.backgroundColor=''"
AutoPostBack="True"
OnSelectedIndexChanged="ddlState_SelectedIndexChanged">asp:DropDownList><asp:RequiredFieldValidator ID="rfvState" runat="server" ErrorMessage="Select Your State" Display="None"
ValidationGroup="vgSubmit"
InitialValue="0"
ControlToValidate="ddlState"
SetFocusOnError="True">asp:RequiredFieldValidator>
ContentTemplate>
asp:UpdatePanel>
td>tr>
<tr><td>Citytd><td>
<asp:UpdatePanel ID="updtpnlCity"
runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlCity"
CssClass="inputddl"
onfocus="this.style.backgroundColor='#ff0000'"
onblur="this.style.backgroundColor=''"
runat="server">asp:DropDownList><asp:RequiredFieldValidator ID="rfvCity" runat="server" ErrorMessage="Select Your City" Display="None" ValidationGroup="vgSubmit"
InitialValue="0"
ControlToValidate="ddlCity"
SetFocusOnError="True">asp:RequiredFieldValidator>
ContentTemplate>
asp:UpdatePanel>
td>tr>
<tr><td>Telephonetd><td><asp:TextBox ID="txtTel" CssClass="inputtxt"
onfocus="this.style.backgroundColor='#ff0000'"
onblur="this.style.backgroundColor=''"
MaxLength="12"
onkeyup="javascript:onlyNum('num')"
runat="server">asp:TextBox><asp:RequiredFieldValidator ID="rfvTel" runat="server" ErrorMessage="Enter Your Telephone" Display="None"
ValidationGroup="vgSubmit"
ControlToValidate="txtTel"
SetFocusOnError="True">asp:RequiredFieldValidator>td>tr>
<tr><td>Emailtd><td><asp:TextBox ID="txtEmail"
CssClass="inputtxt"
onfocus="this.style.backgroundColor='#ff0000'"
onblur="this.style.backgroundColor=''"
runat="server">asp:TextBox><asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="Enter Your Email" Display="None" ValidationGroup="vgSubmit"
ControlToValidate="txtEmail"
SetFocusOnError="True">asp:RequiredFieldValidator><asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="Invalid Email Id" Display="None" ValidationGroup="vgSubmit"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
SetFocusOnError="True">asp:RegularExpressionValidator>td>tr>
<tr><td>Product
Intrestedtd><td><asp:TextBox CssClass="inputtxt"
onfocus="this.style.backgroundColor='#ff0000'"
onblur="this.style.backgroundColor=''"
ID="txtProductInt"
Rows="5"
Columns="15"
TextMode="MultiLine"
runat="server">asp:TextBox><asp:RequiredFieldValidator ID="rfvProductIntrested" runat="server"
ErrorMessage="Enter
Product Intrested" Display="None" ValidationGroup="vgSubmit" ControlToValidate="txtProductInt" SetFocusOnError="True">asp:RequiredFieldValidator>td>tr>
<tr><td> td><td><img src="Captcha.aspx"
alt="Code"
/>td>tr>
<tr>
<td>
Codetd>
<td>
<asp:TextBox ID="txtCode" onfocus="this.style.backgroundColor='#ff0000'" onblur="this.style.backgroundColor=''"
runat="server"
CssClass="inputtxt">asp:TextBox><asp:RequiredFieldValidator ID="rfvCode" runat="server" ControlToValidate="txtCode" Display="None" ErrorMessage="Enter Code Shown in Picture" SetFocusOnError="True"
ValidationGroup="vgSubmit">asp:RequiredFieldValidator>
td>
tr>
<tr><td> td><td><asp:Button ID="btnSubmit"
CssClass="inputbtn"
runat="server"
ValidationGroup="vgSubmit"
Text="SUBMIT"
OnClick="btnSubmit_Click"
/> <input type="reset"
class="inputbtn"
value="RESET"
/>td>tr>
<tr><td colspan="2"><asp:ValidationSummary ID="vsSubmit" runat="server" ShowMessageBox="true" ShowSummary="false" ValidationGroup="vgSubmit" />td>tr>
table>
td>
tr>
table>
div>
form>
body>
html>
using System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Collections.Generic;
using
System.Net;
using
System.Net.Mail;
using
System.Data.SqlClient;
#region [Contact
Us]
//Contact us Form
#endregion
public partial class Contact : System.Web.UI.Page
{
clsDAL
objDAL = new clsDAL(); //Data Access
Layer class object which is already mentioned in my previous post
protected void Page_Load(object
sender, EventArgs e)
{
//Generate
Captcha Image
generateCaptcha();
if
(!IsPostBack)
{
loadState();
ddlState.Items.Insert(0, new ListItem("--Select", "0"));
ddlCity.Items.Insert(0, new ListItem("--Select", "0"));
}
}
protected void btnSubmit_Click(object
sender, EventArgs e)
{
//Send Email
sendMail();
}
private void sendMail()
{
//Check
Captcha Code whether it is proper
if
(generateCaptcha() == true)
{
SmtpClient
smtpC = new SmtpClient();
MailMessage
msg = new MailMessage();
try
{
MailAddress
fromAdd = new MailAddress(txtEmail.Text.Trim().Replace("'", "''"),
txtName.Text.Trim().Replace("'", "''"));
smtpC.Host = "smtp.urdomain.com";
smtpC.Port = 25;
msg.From = fromAdd;
msg.To.Add("xyz@urdomain.com");
msg.Subject = "Enquiry from my website";
msg.CC.Add("abc@xyz.com");
msg.IsBodyHtml = true;
msg.Body = msgBody();
smtpC.UseDefaultCredentials = false;
//smtpC.DeliveryMethod
= SmtpDeliveryMethod.PickupDirectoryFromIis;
smtpC.EnableSsl = false;
smtpC.Credentials = new NetworkCredential("xyz@urdomain.com", "password");
//smtpC.Send(msg);
lblMsg.Visible = true;
lblMsg.Text = "Thank you for Enquiry, we will get back to you
soon!";
lblMsg.ForeColor =
System.Drawing.Color.Black;
//Insert
data into database
addData();
//Reset
Previous Control
resetPreviousControl();
}
catch
(Exception ex)
{
lblMsg.Visible = true;
lblMsg.Text = "There is an Error, please try again!";
lblMsg.ForeColor =
System.Drawing.Color.Red;
}
}
else
{
lblMsg.Visible = true;
lblMsg.Text = "Please enter code shown in picture";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}
//Message Body
private string msgBody()
{
string
strBody = "";
strBody += "
";
strBody += "
";
strBody += "
";
strBody += "
";
strBody += "
";
strBody += "
";
strBody += "
";
strBody += "
";
strBody += "
Name | " + txtName.Text.Trim() + " |
Address | " + txtAddress.Text.Trim() + " |
State | " + ddlState.SelectedItem.Text + " |
City | " + ddlCity.SelectedItem.Text + " |
Telephone | " + txtTel.Text.Trim() + " |
" + txtEmail.Text.Trim() + " | |
Product Intrested | " + txtProductInt.Text.Trim() + " |
return
strBody;
}
protected void loadState()
{
//usp_GetStateMaster
DataTable
dtState = objDAL.getDataTable("select StateId,
StateName from StateMaster WHERE Active = 1 ORDER BY StateName");
if
(dtState.Rows.Count > 0)
{
ddlState.DataSource = dtState;
ddlState.DataValueField = "StateId";
ddlState.DataTextField = "StateName";
ddlState.DataBind();
}
}
protected void loadCity(int
StateId)
{
//usp_GetCityMasterbyState
DataTable
dtCity = objDAL.getDataTable("select CityId,
CityName from CityMaster WHERE Active = 1 AND StateId = " + StateId
+ " ORDER BY CityName");
if
(dtCity.Rows.Count > 0)
{
ddlCity.DataSource = dtCity;
ddlCity.DataValueField = "CityId";
ddlCity.DataTextField = "CityName";
ddlCity.DataBind();
}
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs
e)
{
if
(ddlState.SelectedIndex > 0)
{
loadCity(Convert.ToInt32(ddlState.SelectedValue));
}
else
{
ddlCity.Items.Clear();
ddlCity.Items.Insert(0, new ListItem("--Select", "0"));
}
}
protected bool generateCaptcha()
{
if (!this.IsPostBack)
{
this.Session["CaptchaImageText"] =
GenerateRandomCode();
return
true;
}
else
{
// On a
postback, check the user input.
if
(this.txtCode.Text == this.Session["CaptchaImageText"].ToString())
{
return
true;
}
else
{
//
Clear the input and create a new random code.
this.txtCode.Text
= "";
this.Session["CaptchaImageText"] =
GenerateRandomCode();
return
true;
}
}
}
private string GenerateRandomCode()
{
Random
random = new Random();
string
s = "";
for (int i = 0; i < 6; i++)
s = String.Concat(s,
random.Next(10).ToString());
return
s;
}
protected void resetPreviousControl()
{
txtName.Text = string.Empty;
txtAddress.Text = string.Empty;
ddlState.SelectedIndex = 0;
ddlCity.SelectedIndex = 0;
txtTel.Text = string.Empty;
txtEmail.Text = string.Empty;
txtProductInt.Text = string.Empty;
txtCode.Text = string.Empty;
}
protected void addData()
{
string
strName = txtName.Text.Trim().Replace("'","''");
string
strAddress = txtAddress.Text.Trim().Replace("'",
"''");
int
CityId = Convert.ToInt32(ddlCity.SelectedValue);
string
strTelephone = txtTel.Text.Trim().Replace("'",
"''");
string
strEmail = txtEmail.Text.Trim().Replace("'",
"''");
string
strProductIntrested = txtProductInt.Text.Trim().Replace("'", "''");
try
{
SqlParameter
[] pArray = new SqlParameter[6];
pArray[0] = new SqlParameter("@Name", SqlDbType.Char,
70);
pArray[0].Value = strName;
pArray[1] = new SqlParameter("@Address", SqlDbType.VarChar,
150);
pArray[1].Value = strAddress;
pArray[2] = new SqlParameter("@CityId", SqlDbType.Int);
pArray[2].Value = CityId;
pArray[3] = new SqlParameter("@Telephone", SqlDbType.VarChar,
12);
pArray[3].Value = strTelephone;
pArray[4] = new SqlParameter("@Email", SqlDbType.VarChar,
30);
pArray[4].Value = strEmail;
pArray[5] = new SqlParameter("@ProductIntrested", SqlDbType.VarChar, 500);
pArray[5].Value =
strProductIntrested;
objDAL.ExecuteNonQuery("usp_AddContactUs", pArray);
}
catch (Exception ex)
{
//lblMsg.Text
= ex.Message;
}
}
}
Comments