Creating a ASP.NET Contact Form | WebReference

Creating a ASP.NET Contact Form

By Ryan Butler


[next]

When developing a website for business, personal or organizational use, having a reliable, secure and intuitive way of ascertaining information from your visitors in an effort to better meet their needs, creating and developing a contact form is essential. Creating a form allows website owners to ask for specific information from the visitor in an effort to respond back to them in a way that seems meaningful and useful. However, if precautions are not taken, creating these forms can lead to abuse, misuse and incorrect information getting back to the business, person or organization, and in most cases, leading to a mess of unsolicited electronic mail that wreaks havoc on email systems.

In this article, we will look at creating a ASP.NET contact form that will allow us to capture specific information that we need, validate form fields using client and server side languages to ensure that fields are not empty and then send the results to a specified recipient (s) that will allow them to respond to the visitor in depth when time and information permits. In order to do this, we need to download and install the .NET framework, install Internet Information Services (IIS), understand client-server architecture and its relation to web programming. If you would like to learn how to create such a form, as well as learning about these concepts in-depth please follow along with the article below.

Requirements

In order for us to work effectively, we'll create our form through Internet Information Services (IIS) on our local computer and then transfer the files to our remote host. There are a variety of reasons for taking this approach including: (1) some hosts have low remote connectivity timeouts which makes developing these types of forms difficult to accomplish remotely, (2) you have more flexibility debugging your code locally and (3) most hosts don't allow direct access to their server from within an editor. As a result, we'll need the following software installed on our computer:

  • Windows XP Professional
  • Internet Information Services (IIS)
  • Microsoft.NET Framework (version 2.0 or 3.5 will work)
  • Preferably Visual Web Developer express (free)

Pre Download Information

For the purpose of this article, it's assumed you have Windows XP Professional installed. Continuing, let's first download and install the .NET framework from Microsoft. If you're wondering why we're skipping Internet Information Services (IIS) at the moment, it's because often times if you install IIS before installing the .NET framework, IIS will not install and configure some of its services correctly, including the association it has to the .NET framework, among others.

Download and Install .NET Framework

In order to download and install the .NET framework, open your preferred web browser and follow the web address below:

https://www.asp.net/downloads/essential/

From this page, you'll see two download areas, one for the .NET framework and one for Visual Web Developer Express; choose the .NET framework. Once you click the link, you'll be prompted to save the executable file to a location of your choice, once downloaded, double click the file and follow the steps.

Download and Install Visual Web Developer Express

In order to download Visual Web Developer Express, return to the web address provided above, click the download link, and once again, you'll be prompted to save the executable file to a location of your choice; once downloaded, double click the file and follow the steps.

Installing Internet Information Services (IIS)

You may be wondering at this point, if we need to bother with installing Internet Information Services (IIS) since we already installed the .NET framework and Visual Web Developer. Even though we have the framework installed, which gives us access to the classes, web controls and resources needed to create our form, when working with web applications we still need a web server that is capable of accepting our request from the client's browser, parsing server side web controls, programmatic code and then sending the result back in html so that a web browser can understand and render the web page appropriately. More on the concept client-server architecture will be covered later in the article.

In order to install Internet Information Services (IIS), follow these steps:

  • From your desktop, click Start : Settings : Control Panel
  • In your Control Panel, double click Add or Remove Programs
  • In this window, on the left side, left click Add/Remove Windows Components
  • In the Windows Components Wizard, scroll down until you find Internet Information Services and check the box
  • Left click Next
  • At some point during the install process you'll be prompted for your Windows XP CD which is where IIS resides

Once this is complete, close all windows. We'll need to test and verify the ASP client was registered with IIS.

Testing Local host (IIS)

Before continuing, let's ensure IIS is installed and configured properly to handle ASP.NET. Open your preferred web browser and type the following address:

https://localhost/

If you get an error screen, don't panic. Sometimes during the installation and configuration of the .NET framework & IIS, the ASP client isn't registered with IIS. It's nothing to fret about and can be easily fixed by opening a command line and typing the following:

C:\WINDOWS\Microsoft.NET\Framework\[version]\aspnet_regiis -i

…where version is the version of the .NET framework installed on your computer. Once this is complete, refresh your browser and a welcome screen should show.

Create a Virtual Directory/Application

In the ASP.NET environment, when creating web forms, you'll want to create them within a virtual directory or application in IIS. Associating web forms to a virtual directory or application within IIS allows your application to operate more efficiently and allows your application access to features that makes working with ASP.NET web forms far easier than most server side environments. Such features include:

  • Access to a Web.config file that allows your application to contain information including:
    • Connection string information to a database, default compilation settings such as programming languages (C# or VB.NET), registration of in-house written components and authentication settings.
  • Creation of two Web form files:
    • WebForm.aspx
    • WebForm.aspx.cs

More on the intricacies of these files will be discussed later in the article.

In order to create a virtual directory or application in IIS, you need a physical folder setup on your hard drive. Once completed, follow the steps below to open IIS:

  • From the desktop, click Start : Settings : Control Panel
  • In the Control Panel window, double click Administrative Tools
  • In the Administrative Tools window, double click Internet Information Services
  • In the Internet Information Services window, expand the plus sign next to your computer name, which should show two folders, Web Sites and Default SMTP Server
  • Expand the plus sign next to Web Sites, which should show Default Web Site.
  • To create the virtual directory/application, right click on Default Web Site and select New : Virtual Directory
  • In the Virtual Directory Wizard window, click Next
  • For the alias name, type dev, which is what you'll use to identify your virtual directory/application, and then, click Next
  • For the directory, click Browse and select your physical root folder where you'll be creating the web form
  • For the access permissions, leave as is, and click Next, which should show a finished window, click Finish
  • In the IIS window, you should see your newly created virtual directory/application as well as the files it contains. The directory should be empty.

Close the IIS window and open Visual Web Developer Express; we're ready to begin creating our contact form.

Open Local Web Site from Visual Web Developer Express

When you first open Visual Web Developer Express, a start up screen will show with a default language setting. For the purposes of this article, we'll be using C# (pronounced C-sharp). To open our web application, follow these steps:

  • From the main menu, select File : Open Web Site
  • In the Open Web Site window, on the left side, select Local IIS
  • You should see the virtual directory/application you just created, left click to select it and left click Open

[next]