Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel Dawson 27 posts 47 karma points
    Jul 18, 2011 @ 23:32
    Daniel Dawson
    0

    Contact Form User Control Problem

    Hi there,

    I'm currently creating a contact form user control (I know I could use Contour, but I'd rather make my own). I originally had the contact form coded into the 'Contact' template of my website using inline C# and the asp.net controls which worked fine except it kind of defeats the point of content managment.

    I've created a usercontrol with parameters which allows the user to enter their e-mail address, password, server port and server name when the macro is placed on my contact page in the content section of Umbraco.

    It sort of works except when I test the contact form on my Contact page in the front end and send a message with all fields filled in correctly, it sends the e-mail and then shows the validation error messages for every field of the contact form. I checked my e-mail inbox and the message does send correctly.

    Also, if I leave some fields blank it will still send the message and show all of the validation error messages for each field and repeat the validation error messages for the fields that were left blank. The e-mail was received in my inbox after trying that.

    If I leave all fields blank I get:

    Server Error in '/' Application.

    An invalid character was found in the mail header: '"'.


    Here is the code for the user control...as I said, it worked fine in the 'Contact' template of my website but when it's used as a user control it's not working as I'd like it to:

    .ASCX File

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ContactUserControl.ascx.cs" Inherits="ContactUserControl.ContactUserControl" %>

    <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList" />

        <table id="contact-form">

        <tr>

        <td>Name: <asp:TextBox ID="txtName" CssClass="onelinetxtbox" runat="server"></asp:TextBox></td>

        <td class="validate"><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"

                                                ErrorMessage="You must enter your name" SetFocusOnError="true">*</asp:RequiredFieldValidator></td>

        </tr>

     

        <tr>

        <td>E-mail: <asp:TextBox ID="txtEmail" CssClass="onelinetxtbox" runat="server"></asp:TextBox></td>

        <td class="validate"><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail"

                                                ErrorMessage="You must enter your email address" SetFocusOnError="true">*</asp:RequiredFieldValidator>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail"

                                        ErrorMessage="You must enter a valid email address e.g [email protected])" ValidationExpression=

                                        "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" SetFocusOnError="true">*</asp:RegularExpressionValidator></td>

        </tr>

     

        <tr>

        <td>Subject: <asp:TextBox ID="txtSubject" CssClass="onelinetxtbox" runat="server"></asp:TextBox></td>

        <td class="validate"><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtSubject"

                                        ErrorMessage="You must enter a subject" SetFocusOnError="true">*</asp:RequiredFieldValidator></td>

        </tr>

       <tr>

       <td>Your message: <asp:TextBox ID="txtComments" CssClass="multilinetxtbox" TextMode="MultiLine" runat="server"></asp:TextBox></td>

       <td class="validate"><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtComments"

                                        ErrorMessage="You must enter a message" SetFocusOnError="true">*</asp:RequiredFieldValidator></td>

       </tr>

     

       <tr>

       <td><asp:Button ID="btnSend" runat="server" Text="Send" CssClass="contactsendbtn" /></td>

       </tr>

                                </table>

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

    using System.Net;

     

    namespace ContactUserControl

    {

        public partial class ContactUserControl : System.Web.UI.UserControl

        {

            protected void Page_Load(object sender, EventArgs e)

            {

                btnSend.Click += new EventHandler(btnSend_Click);

            }

     

            protected void btnSend_Click(object sender, EventArgs e)

            {

                string email;

                string name;

                string emailname;

     

                //combine name and email for friendly display name

                name = "\"" + txtName.Text + "\" ";

                email = txtEmail.Text;

     

                emailname = name + email;

     

                SendMail(emailname, txtSubject.Text, txtComments.Text);

            }

     

            private void SendMail(string from, string subject, string body)

            {

                string mailServerName = servername;

                int mailServerPort = portname;

                string toAddress = emailaddress;

                string username = emailaddress;

                string password = emailpassword;

     

                SmtpClient mailClient = new SmtpClient(mailServerName,

                                                   mailServerPort);

                mailClient.Host = mailServerName;

                mailClient.Credentials = new NetworkCredential(username,

                                                               password);

                mailClient.EnableSsl = true;

     

                using (MailMessage message = new MailMessage(from,

                                                             toAddress,

                                                             subject,

                                                             body))

                    mailClient.Send(message);

            }

     

            private string _servername;

            public string servername

            {

                get

                {

                    return _servername;

                }

                set

                {

                    _servername = value;

                }

            }

     

            private int _portname;

            public int portname

            {

                get

                {

                    return _portname;

                }

                set

                {

                    _portname = value;

                }

            }

     

            private string _emailaddress;

            public string emailaddress

            {

                get

                {

                    return _emailaddress;

                }

                set

                {

                    _emailaddress = value;

                }

            }

     

            private string _emailpassword;

            public string emailpassword

            {

                get

                {

                    return _emailpassword;

                }

                set

                {

                    _emailpassword = value;

                }

            }

        }

    }

     

    Any help would be much appreciated,

    Cheers,

    Dan

     

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Jul 19, 2011 @ 13:54
    Tim
    0

    Hiya,

    On your btnSend click event, make sure you check that the form is valid beofre you do anything. To do this, check for Page.IsValid() e.g.

    if (Page.IsValid())
    {
        //do submission stuff here
    }

    If you don't do that, the email will get sent, even if the form isn't valid.

Please Sign in or register to post replies

Write your reply to:

Draft