Click here to Skip to main content
15,905,781 members
Articles / Web Development / ASP.NET
Article

AJAX + ASP.NET 2 + C# Contact us page with Progress indicator

Rate me:
Please Sign up or sign in to vote.
2.42/5 (7 votes)
4 Apr 2008CPOL2 min read 51.7K   1.1K   24   15
Simple AJAX + ASP.NET 2 + C# Contact us page

Introduction

There is nothing in sending emails using asp.net.This is going behind any contact us web page where you fill up all the necessary fields and click to submit it. Then it is sending as an email (Sometimes it may stored to the database as well). Here I am going to create simple AJAX based contact us web page. More than 50% of websites uses traditional methods to implement this and when user clicks on the submit button it will be redirected to the another page or page will be refreshed. But really it is very inconvenient.

This can be nicely handled using AJAX UpdatePanel control. Simply create a contact us web page just drag and drop your content in to the UpdatePanel.Thats all.Ok lets try to do this.

Background

You need ,

Microsoft Visual Studio 2005 or Visual Web Developer

Download AJAX and install

Using the code

Your HTML interface code may look like this (Design it as you want).I have added some CSS class also.
HTML
<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" /> 

<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel id="Panel1" runat="server" Height="238px" Width="492px" 
    BackColor="#D9D1AD">
    <TABLE style="WIDTH: 474px; HEIGHT: 190px">
        <TBODY>
            <TR>
                <TD style="WIDTH: 187px; HEIGHT: 30px">
                    <asp:Label id="lblTo" runat="server" 
                        Text="To :" CssClass="DownText">
                    </asp:Label>
                </TD>
                <TD style="WIDTH: 118px; HEIGHT: 30px">
                    <asp:TextBox id="txtMailTo" runat="server" Height="20px" 
                        Width="219px" BackColor="White" CssClass="DownText" 
                        ReadOnly="True">email@yourdomain.com</asp:TextBox>
                </TD>
                <TD style="WIDTH: 143px; HEIGHT: 30px">
                    <asp:RequiredFieldValidator 
                        id="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="txtMailTo" ErrorMessage="*">
                    </asp:RequiredFieldValidator>
                </TD>
            </TR>
            <TR>
                <TD style="WIDTH: 187px">
                    <asp:Label id="lblEmail" runat="server" 
                        Text="From : (Your Email)" CssClass="DownText">
                    </asp:Label>
                </TD>
                <TD style="WIDTH: 118px">
                    <asp:TextBox id="txtMailFrom" runat="server" 
                        Height="20px" Width="219px" BackColor="White" 
                        CssClass="DownText">
                    </asp:TextBox>
                </TD>
                <TD style="WIDTH: 143px">
                    <asp:RegularExpressionValidator 
                        id="RegularExpressionValidator1" runat="server" 
                        CssClass="redtext" ControlToValidate="txtMailFrom" 
                        ErrorMessage="*" 
                        ValidationExpression=
                        "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
                    </asp:RegularExpressionValidator>
                </TD>
            </TR>
            <TR>
                <TD style="WIDTH: 187px">
                    <asp:Label id="lblSubject" runat="server" 
                        Text="Subject :" CssClass="DownText">
                    </asp:Label>
                </TD>
                <TD style="WIDTH: 118px">
                    <asp:TextBox id="txtSubject" runat="server" Height="20px" 
                        Width="219px" BackColor="White" CssClass="DownText">
                    </asp:TextBox>
                </TD>
                <TD style="WIDTH: 143px">
                    <asp:RequiredFieldValidator id="RequiredFieldValidator3" 
                        runat="server" ControlToValidate="txtSubject" 
                        ErrorMessage="*">
                    </asp:RequiredFieldValidator>
                </TD>
            </TR>
            <TR>
                <TD style="WIDTH: 187px; HEIGHT: 64px">
                    <asp:Label id="lblMessage" runat="server" 
                        Text="Message :" CssClass="DownText">
                    </asp:Label>
                </TD>
                <TD style="WIDTH: 118px; HEIGHT: 64px">
                    <asp:TextBox id="txtMessage" runat="server" Height="54px" 
                        Width="219px" BackColor="White" CssClass="DownText" 
                        TextMode="MultiLine">
                    </asp:TextBox>
                </TD>
                <TD style="WIDTH: 143px; HEIGHT: 64px">
                    <asp:RequiredFieldValidator id="RequiredFieldValidator2" 
                        runat="server" ControlToValidate="txtMessage" 
                        ErrorMessage="*">
                    </asp:RequiredFieldValidator>
                </TD>
            </TR>
            <TR>
                <TD style="WIDTH: 187px">
                </TD>
                <TD style="WIDTH: 118px">
                </TD>
                <TD style="WIDTH: 143px">
                </TD>
            </TR>
            <TR>
                <TD style="WIDTH: 187px; HEIGHT: 61px">
                </TD>
                <TD style="WIDTH: 118px; HEIGHT: 61px">
                   <TABLE style="WIDTH: 162px; HEIGHT: 38px">
                       <TBODY>
                           <TR>
                               <TD style="WIDTH: 100px; HEIGHT: 34px">
                                   <asp:Button id="btnSubmit" 
                                       onclick="btnSubmit_Click" 
                                       runat="server" Text="Submit" 
                                       CssClass="DownText">
                                   </asp:Button>
                               </TD>
                               <TD style="WIDTH: 100px; HEIGHT: 34px">
                                   <asp:Button id="btnClear" 
                                       onclick="btnClear_Click" runat="server"
                                       Text="Clear" CssClass="DownText">
                                   </asp:Button>
                               </TD>
                               <TD style="WIDTH: 100px; HEIGHT: 34px">
                                   <asp:Image id="imgDisplay" runat="server" 
                                       Height="30px" Width="30px">
                                   </asp:Image>
                               </TD>
                           </TR>
                       </TBODY>
                   </TABLE>
                   <asp:UpdateProgress id="UpdateProgress1" runat="server">
                       <ProgressTemplate>
                           <div class="RedText">
                               <img src="Images/indicator.gif" />
                               Sending....
                           </div>
                       </ProgressTemplate>
                   </asp:UpdateProgress> 
               </TD>
               <TD style="WIDTH: 143px; HEIGHT: 61px">
               </TD>
           </TR>
       </TBODY>
   </TABLE>
</asp:Panel> 
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>

I have added a ScriptManager control. You can add only one ScriptManger for one webpage

Within the UpdatePanel create your design (Contact us page design)

To indicate the sending progress add UpdateProgress control out of the design

Now check the .cs file (your C# code)

C#
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.Net.Mail;


public partial class Contacts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

imgDisplay.Visible = false;
}
protected void btnClear_Click(object sender, EventArgs e)
{
// Clear all the fields
txtMailTo.Text = "";
txtMailFrom.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// No need to check whether or not all the fields are empty using
//script. Because already check using regular expressions

// I have used try catch block to send email to the destination

try
{
// Define new mailmessage
MailMessage mMailMessage = new MailMessage();

// assign from address
mMailMessage.From = new MailAddress(txtMailFrom.Text);

// assign to address
mMailMessage.To.Add(new MailAddress(txtMailTo.Text));

//set subject
mMailMessage.Subject = txtSubject.Text;

//set email body
mMailMessage.Body = txtMessage.Text;

// check email type HTML or NOT
mMailMessage.IsBodyHtml = false;

// define new SMTP mail client
SmtpClient mSmtpClient = new SmtpClient();

//send email using defined SMTP client
mSmtpClient.Send(mMailMessage);

// display result to sender
imgDisplay.Visible = true;
imgDisplay.ImageUrl = "images/done.jpg";

}
catch
{
// display ERROR message to sender
imgDisplay.Visible = true;
imgDisplay.ImageUrl = "images/error.jpg";

}

//To implement progress indicator I have used this. 
//If you are view this in internet this is optional
System.Threading.Thread.Sleep(2000);
}
} 

first off you must import using System.Net.Mail; namespace. Then define new SMTP email and collect all the information user entered in your webpage. Finally send all the information to recipient as an email. No page redirection ,page is not refreshing .It is simple and powerful.

//To implement progress indicator I have used this. If you are view this in internet this is optional. If you want to check Progress Indicator add following line to your webpage


System.Threading.Thread.Sleep(2000);

Finally don't forget to configure your web.config file

HTML
<!--

Set configuration to send email using System.Net.Mail();

-->

<system.net>

<mailSettings>

<smtp from="RECIEPIENT-EMAIL-ADDRESS-GOES-HERE">

<network host="127.0.0.1" port="25" userName="" password=""/>

</smtp>

</mailSettings>

</system.net>

Use 127.0.0.1 as network host (this is for localhost)

When you upload keep in mind to change the network host

Conclusion

In this article we learned how to create AJAX enable contact us webpage.

I hope you liked the article, happy coding

About Me

Hi,I am P.W.G.S.Poojanath,from Sri Lanka.I am interesting in Software development.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager
Sri Lanka Sri Lanka
Over 7 years of experience as a Project Manager and Web Developer with serving clients/organizations in Europe/Asia.Dedicated problem-solver with a focus on client satisfaction focused on accelerating business outcomes for client organizations.

Certified Scrum Product Owner (CSPO)

Comments and Discussions

 
QuestionMaster page Pin
devaboy12-Apr-12 12:41
devaboy12-Apr-12 12:41 
Generalmaster page not exists please help me quickly Pin
kardelen228720-Aug-10 10:44
kardelen228720-Aug-10 10:44 
GeneralRe: master page not exists please help me quickly Pin
Poojanath, CSPO7-Oct-10 22:44
Poojanath, CSPO7-Oct-10 22:44 
General.net 3.5 Pin
Poojanath, CSPO13-May-09 20:32
Poojanath, CSPO13-May-09 20:32 
Questionasp.net 3.5 and vb.net Pin
IndieGhost13-May-09 2:19
IndieGhost13-May-09 2:19 
Questionmaster page??? Pin
nihilmem23-Jan-09 2:26
nihilmem23-Jan-09 2:26 
QuestionHow do I set the smtp server Pin
Member 27062764-Aug-08 12:01
Member 27062764-Aug-08 12:01 
Hello Poojanath,

Could you please tell me how I can set the Smtp server info, so that I can use the

SmtpClient mSmtpClient = new SmtpClient();

sentence to send mails from the form?

Thanks,
Marco
AnswerRe: How do I set the smtp server Pin
Poojanath, CSPO4-Aug-08 17:40
Poojanath, CSPO4-Aug-08 17:40 
GeneralNew Images Added Pin
Poojanath, CSPO20-Dec-07 21:58
Poojanath, CSPO20-Dec-07 21:58 
GeneralDefault.master' does not exist. Pin
zpkim18-Sep-07 8:38
zpkim18-Sep-07 8:38 
GeneralRe: Default.master' does not exist. [modified] Pin
Poojanath, CSPO18-Sep-07 17:30
Poojanath, CSPO18-Sep-07 17:30 
GeneralRe: Default.master' does not exist. [modified] Pin
zpkim19-Sep-07 7:11
zpkim19-Sep-07 7:11 
GeneralRe: Default.master' does not exist. Pin
Poojanath, CSPO19-Sep-07 17:43
Poojanath, CSPO19-Sep-07 17:43 
GeneralRe: Default.master' does not exist. Pin
nefeli20096-May-09 1:27
nefeli20096-May-09 1:27 
GeneralRe: Default.master' does not exist. Pin
angry88710-Nov-09 21:17
angry88710-Nov-09 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.