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
    Aug 04, 2011 @ 15:41
    Daniel Dawson
    0

    Contact Form Macro Problem - "Error Reading UserControl"

    Hi guys,

    I've made a contact form for my website which works fine, however I want to use it to be used as a usercontrol so that I can change the mail settings through macro parameters.

    I'm so confused, I have no idea what's going wrong.

    This is the code so far:

    ASCX File

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

    <!--Contact Form and Validation Messages-->

    <div id="contactform" runat="server" visible="true">

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

        <table id="contact-form">

        <tr>

          <td>Name:</td> 

          <td><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:</td> 

          <td><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:</td> 

          <td><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:</td>

         <td><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></td>

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

                                </table>

        </div><!--contact form-->

     

        <!--Thankyou message which is visible when message is sent successfully-->

        <div id="thankYou" runat="server" visible="false">

        <h2>

            <asp:Literal ID="thankyouHeader" runat="server"></asp:Literal>  

        </h2>  

        <asp:Literal ID="thankyouMessage" runat="server"></asp:Literal>    

    </div>

     

    ASCX.cs

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Net;

    using System.Net.Mail;

    using umbraco;

     

    namespace Contact_Form

    {

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

        {

            #region Umbraco Properties

     

            private string _serverName, _emailTo, _userName, _userPassword;

            private int _serverPort;

     

            public string serverName

            {

                get

                {

                    return _serverName;

                }

                set

                {

                    _serverName = value;

                }

            }

     

            public string emailTo

            {

                get

                {

                    return _emailTo;

                }

                set

                {

                    _emailTo = value;

                }

            }

     

            public string userName

            {

                get

                {

                    return _userName;

                }

                set

                {

                    _userName = value;

                }

            }

     

            public string userPassword

            {

                get

                {

                    return _userPassword;

                }

                set

                {

                    _userPassword = value;

                }

            }

     

            public int serverPort

            {

                get

                {

                    return _serverPort;

                }

                set

                {

                    _serverPort = value;

                }

            }

     

            #endregion

            protected void Page_Load(object sender, EventArgs e)

            {

                btnSend.Click += new EventHandler(btnSend_Click);

            }

     

            protected void btnSend_Click(object sender, EventArgs e)

            {

                if (!Page.IsValid)

                {

                    return;

                }

                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)

            {

                /*Set properties from Umbraco into mail settings

                 ===========================================================

                 */

                string mailServerName = serverName;

                int mailServerPort = serverPort;

                string toAddress = emailTo;

                string username = userName;

                string password = userPassword;

     

                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);

     

                /*Set Thankyou text from thankyou div

                    ========================================================

                    */

                thankyouHeader.Text = "Thank you";

                thankyouMessage.Text = "We will contact you shortly.";

     

                /*

                ========================================================

                Hide & Show divs to show thank you message and hide contact form div

                ========================================================

                */

                contactform.Visible = false;         //Hide contact form div

                thankYou.Visible = true;            //show thank you message div

            }

        }

    }using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Net;

    using System.Net.Mail;

    using umbraco;

     

    namespace Contact_Form

    {

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

        {

            #region Umbraco Properties

     

            private string _serverName, _emailTo, _userName, _userPassword;

            private int _serverPort;

     

            public string serverName

            {

                get

                {

                    return _serverName;

                }

                set

                {

                    _serverName = value;

                }

            }

     

            public string emailTo

            {

                get

                {

                    return _emailTo;

                }

                set

                {

                    _emailTo = value;

                }

            }

     

            public string userName

            {

                get

                {

                    return _userName;

                }

                set

                {

                    _userName = value;

                }

            }

     

            public string userPassword

            {

                get

                {

                    return _userPassword;

                }

                set

                {

                    _userPassword = value;

                }

            }

     

            public int serverPort

            {

                get

                {

                    return _serverPort;

                }

                set

                {

                    _serverPort = value;

                }

            }

     

            #endregion

            protected void Page_Load(object sender, EventArgs e)

            {

                btnSend.Click += new EventHandler(btnSend_Click);

            }

     

            protected void btnSend_Click(object sender, EventArgs e)

            {

                if (!Page.IsValid)

                {

                    return;

                }

                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)

            {

                /*Set properties from Umbraco into mail settings

                 ===========================================================

                 */

                string mailServerName = serverName;

                int mailServerPort = serverPort;

                string toAddress = emailTo;

                string username = userName;

                string password = userPassword;

     

                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);

     

                /*Set Thankyou text from thankyou div

                    ========================================================

                    */

                thankyouHeader.Text = "Thank you";

                thankyouMessage.Text = "We will contact you shortly.";

     

                /*

                ========================================================

                Hide & Show divs to show thank you message and hide contact form div

                ========================================================

                */

                contactform.Visible = false;         //Hide contact form div

                thankYou.Visible = true;            //show thank you message div

            }

        }

    }

     

    I then added the aspx, aspx.cs and aspx.designer.cs files to the umbraco usercontrols folder and created a macro called 'Contact Form'.

    The .net usercontrol was then selected and when I clicked on browse properties I keep getting the following error:

    Error reading usercontrols/Contact.ascx

    The following list shows the Public Properties from the Control. By checking the Properties and click the "Save Properties" button at the bottom, umbraco will create the corresponding Macro Elements.

     

    System.Web.HttpParseException (0x80004005): Could not load type 'Contact_Form.Contact'. ---> System.Web.HttpParseException (0x80004005): Could not load type 'Contact_Form.Contact'. ---> System.Web.HttpException (0x80004005): Could not load type 'Contact_Form.Contact'. at System.Web.UI.TemplateParser.GetType(String typeName, Boolean ignoreCase, Boolean throwOnError) at System.Web.UI.TemplateParser.ProcessInheritsAttribute(String baseTypeName, String codeFileBaseTypeName, String src, Assembly assembly) at System.Web.UI.TemplateParser.PostProcessMainDirectiveAttributes(IDictionary parseData) at System.Web.UI.TemplateParser.ProcessException(Exception ex) at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding) at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding) at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding) at System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath) at System.Web.UI.TemplateParser.ParseInternal() at System.Web.UI.TemplateParser.Parse() at System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType() at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() at System.Web.Compilation.BuildProvidersCompiler.PerformBuild() at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean ensureIsUpToDate) at System.Web.UI.TemplateControl.LoadControl(VirtualPath virtualPath) at umbraco.developer.assemblyBrowser.Page_Load(Object sender, EventArgs e)

     

    If anybody could help me out it would be much appreciated as I'm pulling my hair out not knowing what has gone wrong.

    Thanks,

    Daniel

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Aug 04, 2011 @ 15:51
    Tom Fulton
    0

    Hi Daniel,

    Have you tried copying the DLL file to /bin/ also?  I guess theoretically you shouldn't have to since you are copying the codebehind - but I think to do this without the DLL you need to change something in the page directive.  Anyway - try copying the DLL and give it a shot?

    -Tom

  • Daniel Dawson 27 posts 47 karma points
    Aug 04, 2011 @ 16:37
    Daniel Dawson
    0

    Thanks Tom that's exactly what the problem was!

    I I had already tried that but thought I'd give it another go and it's working fine this time!

    Thanks again,

    Daniel

  • Md Johirul Islam 37 posts 57 karma points
    Oct 18, 2011 @ 19:09
    Md Johirul Islam
    0

    I am new. I create a user control name Contact.ascx then i put the all first code into the this file. then i copy the next file code and put it into the Contact.ascx.cs. But the problem is thet when i build the solution it show many error. Please could you infor me more details how do i put this code in it. Thank you.  

  • BJ Patel 84 posts 210 karma points
    Apr 27, 2015 @ 18:03
    BJ Patel
    0

    In User Control try changing CodeBehind to CodeFile

     

    <%@ Control CodeBehind="FileName.ascx...."

     

    <%@ Control CodeFile="FileName.ascx...."

Please Sign in or register to post replies

Write your reply to:

Draft