Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco

This is a Simple Contact From done in C# .Net and also can be used in Umbraco.  Paste the code in to Visual Web Developer for a better view.

The important bits are coment in the second file.  One of the Commented out lines highlights how to get the mailto address from an Umbraco field.

The code Below includes the myContactForm.ascx.cs codebehind file and the myContactForm.ascx file.

1) The myContactForm.ascx file: 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactFormforPublications.ascx.cs" Inherits="usercontrols_ContactFormPublications" %>
<div class="contact-form" id="contactForm" runat="server">
    <h3>Request a Publication</h3>
    <div class='int'>
        <form runat="server">
        <asp:Panel DefaultButton="contactFormSubmit" runat="server">
        <fieldset>
            <div class='element name-placeholder'>
                <label for='formname'>Your name</label>
                <input class="text" name="formname" id="formname" runat="server" value="Your name" />
            </div>
            <div class='element email-placeholder'>
                <label for='formemail'>Your email address</label>
                <input class="text" name="formemail" id="formemail" runat="server" value="Your email address" />
            </div>
           <div class='element message-placeholder'>
                <label for='formpostal'>Postal Address</label>
                <input class="text" name="formpostal" id="formpostal" runat="server" value="Your postal address" />
            </div>
             <div class='element message-placeholder'>
                <label for='formtel'>Telephone Number</label>
                  <input class="text" name="formtel" id="formtel" runat="server" value="Your telephone number" />
            </div>
             <div class='element message-placeholder'>
                <label for='formmessage'>No of Copies of Publication:</label>
                 <input class="text" name="formcopies" id="formcopies" runat="server" value="Number of copies" />
            </div>
            <div class='element message-placeholder'>
                <label  for='formmessage'>My Message</label>
                <textarea rows="1" cols="1" name="formmessage" runat="server" id="formmessage">
                </textarea>
            </div>
            <div class='submit'>
                <asp:ImageButton
                    ID="contactFormSubmit"
                    runat="server"
                    CssClass=""
                    ImageUrl=""
                    AlternateText="Send message" />
            </div>
        </fieldset>
        </asp:Panel>
        </form>
    </div>
</div>
<div class="contact-form" id="contactFormSubmitted" runat="server" visible="false" >
    <h3>Thankyou</h3>
    <div class="int">
        <p>Your Request has been submitted. Thankyou..</p>
    </div>
</div>
2) The myContactForm.ascx.cs file: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.NodeFactory;  //Only used when in conjunction with Umbraco

public partial class usercontrols_ContactFormPublications : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        contactFormSubmit.Click += new ImageClickEventHandler(contactFormSubmit_Click);
        if (IsPostBack)
        {
            contactFormSubmitted.Visible = true;
            contactForm.Visible = false;
        
        }

        // 1. Fill in the 'formmessage' TextArea Box with the URL of the Last page Visited
        formmessage.InnerText = "This is the page I am referring to:\r\n\r\n " + Request.ServerVariables["HTTP_REFERER"] + ""; 
    }

    void contactFormSubmit_Click(object sender, ImageClickEventArgs e)
    {
      
     
        var name = !String.IsNullOrEmpty(formname.Value) ? formname.Value : "(unspecified)";
        var fromAddress = !String.IsNullOrEmpty(formemail.Value) ? formemail.Value : "(unspecified)";
        var message = !String.IsNullOrEmpty(formmessage.Value) ? formmessage.Value : "(unspecified)";
        var postal = !String.IsNullOrEmpty(formpostal.Value) ? formpostal.Value : "(unspecified)";
        var telephone = !String.IsNullOrEmpty(formtel.Value) ? formtel.Value : "(unspecified)";
        var copies = !String.IsNullOrEmpty(formcopies.Value) ? formcopies.Value : "(unspecified)";

        var currentPage = Node.GetCurrent();
         //  2.  If you have an Umbraco field called "mailTo" you can get the maill address using this line:
        //var emailToAddress = UmbracoHelper.getUmbracoProperty(currentPage, "mailTo");

        //  3.  If you are not usin '2' just fill in the 'TO' address here:
        var emailToAddress = "contact@markdownie.me.uk";
        if (String.IsNullOrWhiteSpace(emailToAddress))
        {
            throw new Exception("No address to email to specified");
        }


        // 4.  Fill in the EmailBody from the Form Fields in the ascx
        var emailBody =
            "Name:" + name + System.Environment.NewLine +
            "Email address: " + fromAddress + System.Environment.NewLine +
            "Postal address: " + formpostal + System.Environment.NewLine +
            "Postal telephone: " + formtel + System.Environment.NewLine +
            "Number of Copies Required: " + formtel + System.Environment.NewLine +
            "Publication Details: "  + message + System.Environment.NewLine;
       // 5. Create a New Email
        var email = new Email();
        email.sendEmail(
            subject: "My Subject Matter",
            body: emailBody,
            toAddress: "contact@markdownie.me.uk");
    }
}

Comments

  1. Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco (Informative post...)

    Keep sharing..
    Umbraco Development Company

    Umbraco CMS Development Services

    ReplyDelete

Post a Comment

Popular posts from this blog

Umbraco Razor Sort Nodes Ascending or Descending

Umbraco Razor get Querystring